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