use lib '../modules'; use strict; use Net::SFTP::Foreign; $|=1; print "Connecting….."; my $sftp = Net::SFTP::Foreign->new('ipaddress', user=>'user', password=>'secret'); if (not $sftp) { die("Error: No Connection:$@"); } elsif ($sftp->error) { die("Connect Failed : ".$sftp->status); } else { print "Connected!\n"; if (!$sftp->put("/source/path/to/file", "/remote/target/file ")) { print "Failed to Transfer: ".$sftp->error; } else { print "Transfer Done!!"; } print "\nFinished\n"; } #### Connecting.. # queueing msg len: 5, code:1, id:3 ... [1] # waiting for message... [1] # got it!, len:5, code:2, id:3, status: - Connected! # queueing msg len: 36, code:3, id:0 ... [1] # waiting for message... [1] # got it!, len:13, code:102, id:0, status: - # queueing msg len: 21, code:10, id:1 ... [1] # waiting for message... [1] # got it!, len:24, code:101, id:1, status: 0 # queueing msg len: 6447, code:6, id:2 ... [1] # waiting for message... [1] # queueing msg len: 13, code:4, id:3 ... [2] # waiting for message... [2] Failed to Transfer: Connection to remote server is broken #### Connecting... # queueing msg len: 5, code:1, id:3 ... [1] # waiting for message... [1] Password: # got it!, len:5, code:2, id:3, status: - Connected! # queueing msg len: 36, code:3, id:0 ... [1] # waiting for message... [1] # got it!, len:13, code:102, id:0, status: - # queueing msg len: 21, code:10, id:1 ... [1] # waiting for message... [1] # got it!, len:24, code:101, id:1, status: 0 # queueing msg len: 6447, code:6, id:2 ... [1] # waiting for message... [1] # got it!, len:24, code:101, id:2, status: 0 # queueing msg len: 13, code:4, id:3 ... [1] # waiting for message... [1] # got it!, len:24, code:101, id:3, status: 0 # queueing msg len: 36, code:9, id:4 ... [1] # waiting for message... [1] # got it!, len:24, code:101, id:4, status: 0 Transfer Done!! #### my $timeout = 50; use Expect; $Expect::Exp_Internal = 1; $Expect::Debug = 1; my $conn = Expect->new; $conn->raw_pty(1); $conn->log_user(0); $conn->spawn('/usr/bin/ssh', -l => 'USERNAME', 'IPADDRESS', -s => 'sftp') or die "Cannot spawn : $!"; $conn->expect($timeout, "Password:") or die "Password not requested as expected"; $conn->send("PASSWORD\n"); $conn->expect($timeout, "\n"); my $sftp = Net::SFTP::Foreign->new(transport => $conn); die "unable to stablish SSH connection: ". $sftp->error # The rest is the same as above #### Spawned '/usr/bin/ssh -l USERNAME IPADDRESS -s sftp' spawn id(3) Pid: 11622 Tty: /dev/pts/2 Expect::spawn('Expect=GLOB(0x4053a4dc)','/usr/bin/ssh','-l','username',IPADDRESS,'-s','sftp') called at sftp.cgi line 32 expect(Expect=GLOB(0x4053a4dc) 50 Password:) called... expect(): handling param 'Password:'... expect(): exact match 'Password:'... Starting EXPECT pattern matching... Expect::expect('Expect=GLOB(0x4053a4dc)',50,'Password:') called at sftp. cgi line 35 spawn id(3): beginning expect. Timeout: 50 seconds. Current time: Fri May 30 15:46:23 2008 spawn id(3): list of patterns: #1: -ex `Password:' spawn id(3): Does `' match: pattern #1: -ex `Password:'? No. Waiting for new data (50 seconds)... spawn id(3): new data. spawn id(3): read 10 byte(s). spawn id(3): Does `Password: ' match: pattern #1: -ex `Password:'? YES!! Before match string: `' Match string: `Password:' After match string: ` ' Matchlist: () Returning from expect successfully. spawn id(3): accumulator: ` ' Sending 'PASSWORD\n' to spawn id(3) Expect::print('Expect=GLOB(0x4053a4dc)','PASSWORD\x{a}') called at sftp.cgi line 37 expect(Expect=GLOB(0x4053a4dc) 50 ) called... expect(): handling param ' '... expect(): exact match ' '... Starting EXPECT pattern matching... Expect::expect('Expect=GLOB(0x4053a4dc)',50,'\x{a}') called at sftp.cgi line 38 spawn id(3): beginning expect. Timeout: 50 seconds. Current time: Fri May 30 15:46:26 2008 spawn id(3): list of patterns: #1: -ex `\n' spawn id(3): Does ` ' match: pattern #1: -ex `\n'? No. Waiting for new data (50 seconds)... spawn id(3): new data. spawn id(3): read 1 byte(s). spawn id(3): Does ` \n' match: pattern #1: -ex `\n'? YES!! Before match string: ` ' Match string: `\n' After match string: `' Matchlist: () Returning from expect successfully. spawn id(3): accumulator: `' # queueing msg len: 5, code:1, id:3 ... [1] # waiting for message... [1] # got it!, len:5, code:2, id:3, status: - Connecting Connected..... # queueing msg len: 36, code:3, id:0 ... [1] # waiting for message... [1] # got it!, len:13, code:102, id:0, status: - # queueing msg len: 21, code:10, id:1 ... [1] # waiting for message... [1] # got it!, len:24, code:101, id:1, status: 0 # queueing msg len: 6447, code:6, id:2 ... [1] # waiting for message... [1] # queueing msg len: 13, code:4, id:3 ... [2] # waiting for message... [2] Failed to Transfer: Connection to remote server is broken Closing spawn id(3). Expect::hard_close('Expect=GLOB(0x4053a4dc)') called at ../common/modules/Expect.pm line 1621 Expect::DESTROY('Expect=GLOB(0x4053a4dc)') called at sftp.cgi line 0 eval {...} called at sftp.cgi line 0 spawn id(3) closed. Pid 11622 of spawn id(3) terminated, Status: 0x8D00 # queueing msg len: 13, code:4, id:4 ... [3] # waiting for message... [3]