File: passwd_conn.pl

package info (click to toggle)
libnet-sftp-foreign-perl 1.73%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 472 kB
  • sloc: perl: 5,377; sh: 48; makefile: 7
file content (72 lines) | stat: -rw-r--r-- 1,689 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl

# This sample is obsolete and should not be used as a reference.
#
# Current versions of Net::SFTP::Foreign support password
# authentication as long as the IO::Pty module is installed:
#
#     my $sftp = Net::SFTP::Foreign->new($host,
#                                        user => "me",
#                                        passwd => "quite-secret-passwd");
#     $sftp->error and die "unable to connect ro $host";
#

use strict;
use warnings;

use Expect;
use Net::SFTP::Foreign;

$| = 1;

my $errstr = "unable to stablish SSH connection with remote host";
my $timeout = 60;

@ARGV >= 3 or die <<USAGE;
Usage:

    $0 host user password dir

USAGE

my $host = shift;
my $user = shift;
my $passwd = shift;
my @dir = @ARGV ? @ARGV : ('/');

# initialize an Expect object:
my $conn = Expect->new;
$conn->raw_pty(1);
$conn->log_user(0);

# spawn a new SSH process:
$conn->spawn('/usr/bin/ssh', -l => $user, $host, -s => 'sftp')
    or die $errstr;

# wait for the password prompt:
$conn->expect($timeout, "Password:")
    or die "Password not requested as expected";

$conn->send("$passwd\n");

# SSH echoes the "\n" after the password, remove it from the stream:
$conn->expect($timeout, "\n");

# and finally run SFTP over the ssh connection:
my $sftp = Net::SFTP::Foreign->new(transport => $conn);
$sftp->error and die "$errstr: " . $sftp->error;

# and do whatever you want with it...
for my $dir (@dir) {
    my $ls = $sftp->ls($dir);
    if ($ls) {
        print "$dir\n";
        print "  - $_->{filename}\n" for @$ls;
        print "\n";
    }
    else {
        print STDERR "Unable to retrieve directory listing for '$dir': " . $sftp->error . "\n"
    }
}