LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices

Reply
 
Search this Thread
Old 07-27-2012, 11:35 AM   #1
gjbloom
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
SFTP ls drops connection from cron, but not from command line


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:

Code:
#!/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 command 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:
Code:
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 OpenSSH_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,password.
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:
Code:
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 OpenSSH_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,password.
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
My first suspicion was that something isn't being set in the environment, so I added a setEnv() function to set up the same %ENV that the command-line version has. That didn't work.

My next suspicion was that maybe there is an environmental dependency that has to be set before Perl compile time. So I added this block to the top of the program to make sure %ENV is set up before Perl is invoked:
Code:
# force pre-definition of environment before perl is run
# cron lacks env, and defining env from within perl is ineffective
# for dependencies that are resolved before perl (like libraries)
BEGIN
{
    unless ($ENV{ENV_HAS_BEEN_SET})
    {
        $ENV{'SHELL'}='/bin/bash';
        $ENV{'HISTSIZE'}='1000';
        $ENV{'LD_LIBRARY_PATH'}='/opt/dbms/oracle/product/ora1020_32/lib';
        $ENV{'PATH'}='/opt/local/perl/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R
6/bin:/opt/dbms/oracle/product/ora1020_32/bin:/opt/local/bin:/u/liveproc/bin:.';
        $ENV{'MAIL'}='/var/spool/mail/liveproc';
        $ENV{'INPUTRC'}='/etc/inputrc';
        $ENV{'LANG'}='en_US.UTF-8';
        $ENV{'LOGNAME'}='liveproc';
        $ENV{'LESSOPEN'}='|/usr/bin/lesspipe.sh %s';
        $ENV{'ORACLE_HOME'}='/opt/dbms/oracle/product/ora1020_32';
        $ENV{'G_BROKEN_FILENAMES'}='1';
        $ENV{'ENV_HAS_BEEN_SET'}='1';

        exec 'env', $^X, $0, @ARGV;

        # if the exec failed, we don't want to run anyway
        die "Exec failed for 'env', $^X, $0, @ARGV\n";
    }
}
Still fails. We don't have strace available (damn security fanatics). I used ldd to verify that perl itself has the same libraries in both command-line and cron environments.

I am at the end of my rope. Can anyone offer a guess about what is going on here?
 
Old 07-29-2012, 12:27 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,953
Blog Entries: 54

Rep: Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733
If you 'diff --side-by-side "successful command-line-invoked run" "at now";' you get:
Code:
lxdenvmap162: sftp: Sending SSH2_FXP_INIT                       lxdenvmap162: sftp: Sending SSH2_FXP_INIT
lxdenvmap162: sftp: Remote version: 3                           lxdenvmap162: sftp: Remote version: 3
lxdenvmap162: sftp: Sent message T:11 I:0                       lxdenvmap162: sftp: Sent message T:11 I:0
lxdenvmap162: sftp: Sent message T:12 I:1                     | lxdenvmap162: channel 1: rcvd eof
lxdenvmap162: sftp: Received reply T:104 I:1                  | lxdenvmap162: channel 1: output open -> drain
lxdenvmap162: sftp: Received 11 SSH2_FXP_NAME responses       | lxdenvmap162: channel 1: obuf empty
lxdenvmap162: sftp: Sent message T:12 I:2                     | lxdenvmap162: channel 1: output drain -> closed
lxdenvmap162: sftp: Received reply T:101 I:2                  | lxdenvmap162: channel 1: close_write
lxdenvmap162: sftp: Received SSH2_FXP_STATUS 1                | lxdenvmap162: channel 1: rcvd close
lxdenvmap162: sftp: Sent message T:4 I:3                      | lxdenvmap162: channel 1: input open -> closed
.                                                             | lxdenvmap162: channel 1: close_read
..                                                            | lxdenvmap162: channel 1: send close
XTRT.#CO.STRATA.RSKASAIF                                      | lxdenvmap162: channel 1: full closed
XTRT.#CO.STRATA.RSKASAIF.bkp                                  / Connection closed at ./problem.pl line 13
so it "rcvd eof" whatever that is. Maybe run a SSH daemon on the remote machine but at a different port ("-p 9999") and make it spit out debug output ("-d -d -d")? Maybe the output shows something.
 
Old 08-06-2012, 09:41 AM   #3
gjbloom
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Still have no idea what is behind the dropped connections using Net::SFTP. But I switched over to Net::SFTP::Foreign, which is a pure-Perl implementation, and that works just fine.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gawk works from command line but not from cron fantasygoat Linux - Server 3 10-25-2011 01:18 PM
How to use scp or sftp command line with a key file astroboiii Linux - Server 4 10-07-2010 11:27 AM
Works from command line but not from the cron WingnutOne Linux - General 8 11-08-2007 08:18 AM
btdownload*.py, works from Command Line, not from cron SkipHuffman Linux - Software 11 12-30-2005 02:55 PM
ls command closes sftp connection SpecialK5106 Linux - Networking 4 11-03-2003 02:18 AM


All times are GMT -5. The time now is 11:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration