Skip Menu |

This queue is for tickets about the Net-SSH-Perl CPAN distribution.

Report information
The Basics
Id: 4597
Status: rejected
Priority: 0/
Queue: Net-SSH-Perl

People
Owner: Nobody in particular
Requestors: peter [...] adpm.de
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in:
  • 1.23
  • 1.24
Fixed in: (no value)



Subject: Net::SSH::Perl not working from cron
I have a problem with Net::SFTP / Net::SSH::Perl (both the old and the current versions) on Perl 5.6.0 and Linux 2.4.x The attached program sftptest.pl tries to connect to a server and do a simple 'ls' there. It works absolutely flawlessly when run interactively by a regular user, but when the very same user tries to run it from cron he does not get any answers from the server. Another strange behaviour: when root tries to run the program from cron it works as expected. It happens on three machines with similar (but not identical) setup. The attached files sftptest-interactive.log and sftptest-via-cron.log show the differences. They result from a shell script that calls sftptest.pl after setting all environment variables to the values from the cron environment. Somehow the connection seems to get closed at about the time when SSH2_FXP_INIT gets sent. Can you give me any hint or clue how to make Net::SFTP run from cron too ? I'd really love to use Net::SFTP instead of doing some dirty tricks with Expect or calling the sftp program directly. Thanks in advance for your help Peter Marschall ---- sftptest.pl ---- #!/usr/bin/perl -w use Net::SFTP; use strict; my $server = $ARGV[0] || ''; my $user = $ARGV[1] || ''; my $password = $ARGV[2] || ''; my $dir = $ARGV[3] || ''; my @result = (); print STDERR "Environment:\n"; map { print STDERR "\t$_ = $ENV{$_}\n"; } keys(%ENV); print STDERR "Specials:\n"; print STDERR "\t\$\$ = ". (defined($$) ? $$ : '<undef>') ."\n"; print STDERR "\t\$\< = ". (defined($<) ? $< : '<undef>') ."\n"; print STDERR "\t\$\> = ". (defined($>) ? $> : '<undef>') ."\n"; print STDERR "\t\$\/ = ". (defined($/) ? $/ : '<undef>') ."\n"; print STDERR "\t\$\\ = ". (defined($\) ? $\ : '<undef>') ."\n"; print STDERR "\t\$\~ = ". (defined($~) ? $~ : '<undef>') ."\n"; print STDERR "\t\$\% = ". (defined($%) ? $% : '<undef>') ."\n"; print STDERR "\t\$\= = ". (defined($=) ? $= : '<undef>') ."\n"; print STDERR "\t\$\- = ". (defined($-) ? $- : '<undef>') ."\n"; print STDERR "\t\$\, = ". (defined($,) ? $, : '<undef>') ."\n"; print STDERR "\t\$\| = ". (defined($|) ? $| : '<undef>') ."\n"; print STDERR "\t\$\! = ". (defined($!) ? $! : '<undef>') ."\n"; print STDERR "\t\$\? = ". (defined($?) ? $? : '<undef>') ."\n"; my $sftp = Net::SFTP->new($server, user => $user || '', password => $password || '', debug => 1) or die "error creating SFTP object: $@"; die "ls: $@" if (!defined(eval { @result = $sftp->ls($dir) })); print STDERR "Files:\n"; map { print STDERR "\t".$_->{filename}."\n"; } @result; # EOF Show quoted text
----- results from interactive run ---- Environment: _ = /usr/local/src/Meta/test/sftptest.pl HOSTTYPE = i386 HOME = /home/metaconn SHLVL = 1 OSTYPE = linux TERM = dumb PWD = /home/metaconn HOSTNAME = lx0042 MACHTYPE = i386-suse-linux LOGNAME = metaconn PATH = /usr/bin:/bin SHELL = /bin/sh Specials: $$ = 22827 $< = 500 $> = 500 $/ = $\ = <undef> $~ = STDOUT $% = 0 $= = 60 $- = 0 $, = <undef> $| = 0 $! = $? = 0 lx0042: Reading configuration data /home/metaconn/.ssh/config lx0042: Reading configuration data /etc/ssh_config lx0042: Connecting to sl00028.weimar.teagnetkom.de, port 22. lx0042: Remote protocol version 1.99, remote software version OpenSSH_3.5p1 lx0042: Net::SSH::Perl Version 1.24, protocol version 2.0. lx0042: No compat match: OpenSSH_3.5p1. lx0042: Connection established. lx0042: Sent key-exchange init (KEXINIT), wait response. lx0042: Algorithms, c->s: 3des-cbc hmac-sha1 none lx0042: Algorithms, s->c: 3des-cbc hmac-sha1 none lx0042: Entering Diffie-Hellman Group 1 key exchange. lx0042: Sent DH public key, waiting for reply. lx0042: Received host key, type 'ssh-dss'. lx0042: Host 'sl00028.weimar.teagnetkom.de' is known and matches the host key. lx0042: Computing shared secret key. lx0042: Verifying server signature. lx0042: Waiting for NEWKEYS message. lx0042: Enabling incoming encryption/MAC/compression. lx0042: Send NEWKEYS, enable outgoing encryption/MAC/compression. lx0042: Sending request for user-authentication service. lx0042: Service accepted: ssh-userauth. lx0042: Trying empty user-authentication request. lx0042: Authentication methods that can continue: publickey,password. lx0042: Next method to try is publickey. lx0042: Next method to try is password. lx0042: Trying password authentication. lx0042: Login completed, opening dummy shell channel. lx0042: channel 0: new [client-session] lx0042: Requesting channel_open for channel 0. lx0042: channel 0: open confirm rwindow 0 rmax 32768 lx0042: Got channel open confirmation, requesting shell. lx0042: Requesting service shell on channel 0. lx0042: channel 1: new [client-session] lx0042: Requesting channel_open for channel 1. lx0042: Sending subsystem: sftp lx0042: Requesting service subsystem on channel 1. lx0042: channel 1: open confirm rwindow 0 rmax 32768 lx0042: sftp: Sending SSH2_FXP_INIT lx0042: sftp: Remote version: 3 lx0042: sftp: Sent message T:11 I:0 lx0042: sftp: Sent message T:12 I:1 lx0042: sftp: Received reply T:104 I:1 lx0042: sftp: Received 30 SSH2_FXP_NAME responses lx0042: sftp: Sent message T:12 I:2 lx0042: sftp: Received reply T:101 I:2 lx0042: sftp: Received SSH2_FXP_STATUS 1 lx0042: sftp: Sent message T:4 I:3 Files: . .. dms_mobil_031023223033.txt dms_mobil_031022152659.txt dms_mobil_031022223035.txt dms_mobil_031024223039.txt dms_mobil_031027223039.txt
----- results from run under cron ----- Environment: _ = /usr/local/src/Meta/test/sftptest.pl HOSTTYPE = i386 HOME = /home/metaconn SHLVL = 1 OSTYPE = linux TERM = dumb PWD = /home/metaconn HOSTNAME = lx0042 MACHTYPE = i386-suse-linux LOGNAME = metaconn PATH = /usr/bin:/bin SHELL = /bin/sh Specials: $$ = 22796 $< = 500 $> = 500 $/ = $\ = <undef> $~ = STDOUT $% = 0 $= = 60 $- = 0 $, = <undef> $| = 0 $! = $? = 0 lx0042: Reading configuration data /home/metaconn/.ssh/config lx0042: Reading configuration data /etc/ssh_config lx0042: Connecting to sl00028.weimar.teagnetkom.de, port 22. lx0042: Remote protocol version 1.99, remote software version OpenSSH_3.5p1 lx0042: Net::SSH::Perl Version 1.24, protocol version 2.0. lx0042: No compat match: OpenSSH_3.5p1. lx0042: Connection established. lx0042: Sent key-exchange init (KEXINIT), wait response. lx0042: Algorithms, c->s: 3des-cbc hmac-sha1 none lx0042: Algorithms, s->c: 3des-cbc hmac-sha1 none lx0042: Entering Diffie-Hellman Group 1 key exchange. lx0042: Sent DH public key, waiting for reply. lx0042: Received host key, type 'ssh-dss'. lx0042: Host 'sl00028.weimar.teagnetkom.de' is known and matches the host key. lx0042: Computing shared secret key. lx0042: Verifying server signature. lx0042: Waiting for NEWKEYS message. lx0042: Enabling incoming encryption/MAC/compression. lx0042: Send NEWKEYS, enable outgoing encryption/MAC/compression. lx0042: Sending request for user-authentication service. lx0042: Service accepted: ssh-userauth. lx0042: Trying empty user-authentication request. lx0042: Authentication methods that can continue: publickey,password. lx0042: Next method to try is publickey. lx0042: Next method to try is password. lx0042: Trying password authentication. lx0042: Login completed, opening dummy shell channel. lx0042: channel 0: new [client-session] lx0042: Requesting channel_open for channel 0. lx0042: channel 0: open confirm rwindow 0 rmax 32768 lx0042: Got channel open confirmation, requesting shell. lx0042: Requesting service shell on channel 0. lx0042: channel 1: new [client-session] lx0042: Requesting channel_open for channel 1. lx0042: Sending subsystem: sftp lx0042: Requesting service subsystem on channel 1. lx0042: channel 1: open confirm rwindow 0 rmax 32768 lx0042: sftp: Sending SSH2_FXP_INIT Use of uninitialized value in numeric eq (==) at /usr/lib/perl5/site_perl/5.6.0/Net/SSH/Perl/SSH2.pm line 309. Connection closed at /usr/local/src/Meta/test/sftptest.pl line 31
Show quoted text
> The attached program sftptest.pl tries to connect to a server and do a > simple 'ls' there. > It works absolutely flawlessly when run interactively by a regular > user, but when the very same user tries to run it from cron he does > not get any answers from the server. > Another strange behaviour: when root tries to run the program from > cron it works as expected. > > It happens on three machines with similar (but not identical) setup.
When something doesn't work from cron that usually means that there's a difference in the environment the code is being run under. For some reason, Net::SSH::Perl is looking at the TERM env var, so you could try setting that to the same thing as when it's run manually, and see if that makes a difference.
From: peter [...] adpm.de
[DROLSKY - Wed Feb 11 19:26:29 2004]: Show quoted text
> When something doesn't work from cron that usually means that there's
a Show quoted text
> difference in the environment the code is being run under. > > For some reason, Net::SSH::Perl is looking at the TERM env var, so
you Show quoted text
> could try setting that to the same thing as when it's run manually,
and Show quoted text
> see if that makes a difference.
I did this already. The original bug report contains a test script that prints the environment as well as some other Perl variables and then performs the connection test. Also in the bug report are the results of an interactive run and a run from cron. In both cases TERM is set as well as HOME which is another environment variable that Net::SSH::Perl might use. Result: same environment, different results.
Date: Sat, 6 Mar 2004 02:11:14 -0600 (CST)
From: Dave Rolsky <autarch [...] urth.org>
To: Guest via RT <bug-Net-SSH-Perl [...] rt.cpan.org>
Subject: Re: [cpan #4597] Net::SSH::Perl not working from cron
RT-Send-Cc:
On Fri, 5 Mar 2004, Guest via RT wrote: Show quoted text
> I did this already. > The original bug report contains a test script that prints the > environment as well as some other Perl variables and then performs the > connection test. Also in the bug report are the results of an > interactive run and a run from cron. In both cases TERM is set as well > as HOME which is another environment variable that Net::SSH::Perl might > use. > > Result: same environment, different results.
Well, _something_ in the environment must be different! It's not like the code is designed to _not_ work when run as a cron job. I don't know what else to suggest looking at, though. Debugging these sorts of problems can be incredibly painful. Maybe try running it via strace through cron and looking a the trace. It's a trace of C-level calls, but it might help you find where things break down. -dave /*======================= House Absolute Consulting www.houseabsolute.com =======================*/
From: peter [...] adpm.de
Hi, [autarch@urth.org - Sat Mar 6 03:11:22 2004]: Show quoted text
> Well, _something_ in the environment must be different! It's not
like the Show quoted text
> code is designed to _not_ work when run as a cron job. I don't know
what Show quoted text
> else to suggest looking at, though. Debugging these sorts of
problems can Show quoted text
> be incredibly painful. Maybe try running it via strace through cron
and Show quoted text
> looking a the trace. It's a trace of C-level calls, but it might
help you Show quoted text
> find where things break down.
I strace'd the two alternatives and found only one difference in the runtime environment (the %ENV environment was definitively the same): The version in cron had one file descriptor more open (although I was not able to determine which one). Then I played around a bit with input and output redirection and found the solution: open(STDIN, '<','/dev/null'); just before the Net::SFTP Constructor did the trick. But IMHO this is only a workaround (aka. dirty hack) to a bug in Net::SSH::Perl / Net::SFTP that expects STDIN to be open. It seems to be closed in cron and that made my script fail. Maybe you can find a better solution than this crude workaround that takes care whether STDIO is open, ... Hope this helped Peter Peter
From: peter [...] adpm.de
Hi, Too mauch trust in the Perl documentation (man perlfunc: close) Instead of a simple open(STDIN, '<','/dev/null'); you need close(STDIN); open(STDIN, '<','/dev/null'); to make it work in cron. Is it possible the problem has something to do with $. ? Peter
Date: Mon, 15 Mar 2004 14:53:13 -0600 (CST)
From: Dave Rolsky <autarch [...] urth.org>
To: Guest via RT <bug-Net-SSH-Perl [...] rt.cpan.org>
Subject: Re: [cpan #4597] Net::SSH::Perl not working from cron
RT-Send-Cc:
On Mon, 15 Mar 2004, Guest via RT wrote: Show quoted text
> Then I played around a bit with input and output redirection and found > the solution: > open(STDIN, '<','/dev/null'); > just before the Net::SFTP Constructor did the trick. > > But IMHO this is only a workaround (aka. dirty hack) to a bug in > Net::SSH::Perl / Net::SFTP that expects STDIN to be open. > It seems to be closed in cron and that made my script fail. > > Maybe you can find a better solution than this crude workaround that > takes care whether STDIO is open, ...
For some reason the code dups STDIN, STDOUT, and STDERR when the cmd, open2, or shell methods are called. Why does it do this? I have no clue. -dave /*======================= House Absolute Consulting www.houseabsolute.com =======================*/