Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Net::SFTP disconnect

by gregor-e (Beadle)
on Jul 26, 2012 at 23:48 UTC ( [id://983968]=perlquestion: print w/replies, xml ) Need Help??

gregor-e has asked for the wisdom of the Perl Monks concerning the following question:

Net::SFTP is dropping connection when my program invokes ls, but only when the program is run by cron or 'at now'. Here's an example that demonstrates:

#!/opt/local/perl/bin/perl -w use strict; use Net::SFTP; my $sftp = Net::SFTP->new("serverURL.com", user => "myUser", password => 'myPassword', debug => 1); my $targetDir = "/ftp/pub"; # connection dropped at this line when run from cron, but not from com +mand line my $lsHashRef = $sftp->ls($targetDir); map { my $filename = $_->{filename}; print "$filename\n"; } sort {$a->{filename} cmp $b->{filename}} @$lsHashRef; print "Done!\n";

The debug output from a successful command-line-invoked run:

lxdenvmap162: Reading configuration data /u/liveproc/.ssh/config lxdenvmap162: Reading configuration data /etc/ssh_config lxdenvmap162: Connecting to lxdenvmpc366.qintra.com, port 22. lxdenvmap162: Remote version string: SSH-2.0-OpenSSH_4.3 lxdenvmap162: Remote protocol version 2.0, remote software version Ope +nSSH_4.3 lxdenvmap162: Net::SSH::Perl Version 1.30, protocol version 2.0. lxdenvmap162: No compat match: OpenSSH_4.3. lxdenvmap162: Connection established. lxdenvmap162: Sent key-exchange init (KEXINIT), wait response. lxdenvmap162: Algorithms, c->s: 3des-cbc hmac-sha1 none lxdenvmap162: Algorithms, s->c: 3des-cbc hmac-sha1 none lxdenvmap162: Entering Diffie-Hellman Group 1 key exchange. lxdenvmap162: Sent DH public key, waiting for reply. lxdenvmap162: Received host key, type 'ssh-dss'. lxdenvmap162: Host 'lxdenvmpc366.qintra.com' is known and matches the +host key. lxdenvmap162: Computing shared secret key. lxdenvmap162: Verifying server signature. lxdenvmap162: Waiting for NEWKEYS message. lxdenvmap162: Enabling incoming encryption/MAC/compression. lxdenvmap162: Send NEWKEYS, enable outgoing encryption/MAC/compression +. lxdenvmap162: Sending request for user-authentication service. lxdenvmap162: Service accepted: ssh-userauth. lxdenvmap162: Trying empty user-authentication request. lxdenvmap162: Authentication methods that can continue: publickey,pass +word. lxdenvmap162: Next method to try is publickey. lxdenvmap162: Next method to try is password. lxdenvmap162: Trying password authentication. lxdenvmap162: Login completed, opening dummy shell channel. lxdenvmap162: channel 0: new [client-session] lxdenvmap162: Requesting channel_open for channel 0. lxdenvmap162: channel 0: open confirm rwindow 0 rmax 32768 lxdenvmap162: channel 1: new [client-session] lxdenvmap162: Requesting channel_open for channel 1. lxdenvmap162: Sending subsystem: sftp lxdenvmap162: Requesting service subsystem on channel 1. lxdenvmap162: channel 1: open confirm rwindow 0 rmax 32768 lxdenvmap162: sftp: Sending SSH2_FXP_INIT lxdenvmap162: sftp: Remote version: 3 lxdenvmap162: sftp: Sent message T:11 I:0 lxdenvmap162: sftp: Sent message T:12 I:1 lxdenvmap162: sftp: Received reply T:104 I:1 lxdenvmap162: sftp: Received 11 SSH2_FXP_NAME responses lxdenvmap162: sftp: Sent message T:12 I:2 lxdenvmap162: sftp: Received reply T:101 I:2 lxdenvmap162: sftp: Received SSH2_FXP_STATUS 1 lxdenvmap162: sftp: Sent message T:4 I:3 . .. XTRT.#CO.STRATA.RSKASAIF XTRT.#CO.STRATA.RSKASAIF.bkp XTRT.#EN.STRATA.RSKASAIF XTRT.#ES.STRATA.RSKASAIF XTRT.#NM.STRATA.RSKASAIF XTRT.#OR.STRATA.RSKASAIF XTRT.#UT.STRATA.RSKASAIF XTRT.#WA.STRATA.RSKASAIF processed Done!

The debug output from a failed run using 'at now' to run it:

lxdenvmap162: Reading configuration data /u/liveproc/.ssh/config lxdenvmap162: Reading configuration data /etc/ssh_config lxdenvmap162: Connecting to lxdenvmpc366.qintra.com, port 22. lxdenvmap162: Remote version string: SSH-2.0-OpenSSH_4.3 lxdenvmap162: Remote protocol version 2.0, remote software version Ope +nSSH_4.3 lxdenvmap162: Net::SSH::Perl Version 1.30, protocol version 2.0. lxdenvmap162: No compat match: OpenSSH_4.3. lxdenvmap162: Connection established. lxdenvmap162: Sent key-exchange init (KEXINIT), wait response. lxdenvmap162: Algorithms, c->s: 3des-cbc hmac-sha1 none lxdenvmap162: Algorithms, s->c: 3des-cbc hmac-sha1 none lxdenvmap162: Entering Diffie-Hellman Group 1 key exchange. lxdenvmap162: Sent DH public key, waiting for reply. lxdenvmap162: Received host key, type 'ssh-dss'. lxdenvmap162: Host 'lxdenvmpc366.qintra.com' is known and matches the +host key. lxdenvmap162: Computing shared secret key. lxdenvmap162: Verifying server signature. lxdenvmap162: Waiting for NEWKEYS message. lxdenvmap162: Enabling incoming encryption/MAC/compression. lxdenvmap162: Send NEWKEYS, enable outgoing encryption/MAC/compression +. lxdenvmap162: Sending request for user-authentication service. lxdenvmap162: Service accepted: ssh-userauth. lxdenvmap162: Trying empty user-authentication request. lxdenvmap162: Authentication methods that can continue: publickey,pass +word. lxdenvmap162: Next method to try is publickey. lxdenvmap162: Next method to try is password. lxdenvmap162: Trying password authentication. lxdenvmap162: Login completed, opening dummy shell channel. lxdenvmap162: channel 0: new [client-session] lxdenvmap162: Requesting channel_open for channel 0. lxdenvmap162: channel 0: open confirm rwindow 0 rmax 32768 lxdenvmap162: channel 1: new [client-session] lxdenvmap162: Requesting channel_open for channel 1. lxdenvmap162: Sending subsystem: sftp lxdenvmap162: Requesting service subsystem on channel 1. lxdenvmap162: channel 1: open confirm rwindow 0 rmax 32768 lxdenvmap162: sftp: Sending SSH2_FXP_INIT lxdenvmap162: sftp: Remote version: 3 lxdenvmap162: sftp: Sent message T:11 I:0 lxdenvmap162: channel 1: rcvd eof lxdenvmap162: channel 1: output open -> drain lxdenvmap162: channel 1: obuf empty lxdenvmap162: channel 1: output drain -> closed lxdenvmap162: channel 1: close_write lxdenvmap162: channel 1: rcvd close lxdenvmap162: channel 1: input open -> closed lxdenvmap162: channel 1: close_read lxdenvmap162: channel 1: send close lxdenvmap162: channel 1: full closed Connection closed at ./problem.pl line 13

I know this isn't likely to be Perl-specific, but please, oh mighty monks, do any of you have a guess about why this is happening?

Replies are listed 'Best First'.
Re: Net::SFTP disconnect
by flexvault (Monsignor) on Jul 27, 2012 at 08:22 UTC

    gregor-e,

    You said a guess was okay, so here goes. When you run from 'cron' you have to make sure that all environmental variables are setup correctly. Doing a super search on 'cron environment variable' shows multiple considerations and solutions. Hope this helps.

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

Re: Net::SFTP disconnect
by runrig (Abbot) on Jul 27, 2012 at 17:06 UTC
Re: Net::SFTP disconnect
by flexvault (Monsignor) on Jul 27, 2012 at 17:31 UTC

    gregor-e,

    Just to point out what you probably already know, I compared the 'good' and 'bad' outputs you provided, and 'bad' shows that you received 'channel 1: rcvd eof' from the remote server. Someone more familiar with Net::SFTP may give a hint on why the remove server is acting differently on 'cron' or 'at now'.

    . . . lxdenvmap162: channel 1: open confirm rwindow 0 rmax 32768 lxdenvmap162: sftp: Sending SSH2_FXP_INIT lxdenvmap162: sftp: Remote version: 3 lxdenvmap162: sftp: Sent message T:11 I:0 lxdenvmap162: sftp: Sent message T:12 I:1 lxdenvmap162: sftp: Received reply T:104 I:1 lxdenvmap162: sftp: Received 11 SSH2_FXP_NAME responses lxdenvmap162: sftp: Sent message T:12 I:2 lxdenvmap162: sftp: Received reply T:101 I:2 . . .
    . . . lxdenvmap162: channel 1: open confirm rwindow 0 rmax 32768 lxdenvmap162: sftp: Sending SSH2_FXP_INIT lxdenvmap162: sftp: Remote version: 3 lxdenvmap162: sftp: Sent message T:11 I:0 lxdenvmap162: channel 1: rcvd eof #### <<<<<< lxdenvmap162: channel 1: output open -> drain lxdenvmap162: channel 1: obuf empty . . .

    I use 'rsync' with public/private keys and 'ssh' to encrypt the session. It's not a pure-Perl solution, but has worked for years. Maybe answer this post on Monday, when it may be picked up in RAT, and more monks are available. Things that you could look into:

    • Configuration parameter about timing ( cron maybe faster then the shell??? )
    • You may want to look at http://www.perlmonks.org/?node_id=977128"> which talks about '...Net::SSH2 was designed to replace the old sftp and ssh module..." (again through super seacrch).

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

Re: Net::SFTP disconnect
by gregor-e (Beadle) on Jul 27, 2012 at 14:19 UTC
    I failed to mention that one of the things I have tried to get this to work was to add a setEnv() function to establish all the %ENV that is missing under cron. Alas, it made no difference.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://983968]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found