与网络失去连接:: SSH :: Perl的

问题描述:

SOLUTION低于与网络失去连接:: SSH :: Perl的

我们有提取数据到CSV,它上传到另一台服务器,然后需要连接到其它服务器和呼叫的ETL系统一个java jar将csv加载到memcache中。我有一个脚本可以执行此操作的每一步,但会丢失最后一步的SSH连接。远程机器上的进程继续并完成。

我正在使用Net :: SSH :: Perl,并且在短时间运行后收到“连接失败:由对等方重置连接”错误。我煮的脚本到这一点,并复制结果:

#!/usr/bin/perl 

use strict; 
use Net::SSH::Perl; 
use Log::Log4perl; 

my ($stdout, $stderr, $exit, $ssh); 

$ssh = Net::SSH::Perl->new('sshost', 
     identity_files => ['/path/to/key.rsa'], 
     protocol => 2, 
     debug => 1); 

$ssh->login('user'); 

my $cmd = "java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl"; 

$ssh->register_handler("stdout", sub { 
     my($channel, $buffer) = @_; 
     print "STDOUT: ", $buffer->bytes; 
}); 

$ssh->register_handler("stderr", sub { 
     my($channel, $buffer) = @_; 
     print "STDERR: ", $buffer->bytes; 
}); 

$ssh->cmd("cd /usr/local/loader; $cmd"); 

的SSH调试信息,我得到的是:那么

localhost: Reading configuration data /home/user/.ssh/config 
localhost: Reading configuration data /etc/ssh_config 
localhost: Connecting to sshost, port 22. 
localhost: Remote protocol version 2.0, remote software version OpenSSH_4.3 
localhost: Net::SSH::Perl Version 1.34, protocol version 2.0. 
localhost: No compat match: OpenSSH_4.3. 
localhost: Connection established. 
localhost: Sent key-exchange init (KEXINIT), wait response. 
localhost: Algorithms, c->s: 3des-cbc hmac-sha1 none 
localhost: Algorithms, s->c: 3des-cbc hmac-sha1 none 
localhost: Entering Diffie-Hellman Group 1 key exchange. 
localhost: Sent DH public key, waiting for reply. 
localhost: Received host key, type 'ssh-dss'. 
localhost: Host 'sshost' is known and matches the host key. 
localhost: Computing shared secret key. 
localhost: Verifying server signature. 
localhost: Waiting for NEWKEYS message. 
localhost: Send NEWKEYS. 
localhost: Enabling encryption/MAC/compression. 
localhost: Sending request for user-authentication service. 
localhost: Service accepted: ssh-userauth. 
localhost: Trying empty user-authentication request. 
localhost: Authentication methods that can continue: publickey,gssapi-with-mic. 
localhost: Next method to try is publickey. 
localhost: Trying pubkey authentication with key file '/path/to/key.rsa' 
localhost: Login completed, opening dummy shell channel. 
localhost: channel 0: new [client-session] 
localhost: Requesting channel_open for channel 0. 
localhost: channel 0: open confirm rwindow 0 rmax 32768 
localhost: Got channel open confirmation, requesting shell. 
localhost: Requesting service shell on channel 0. 
localhost: channel 1: new [client-session] 
localhost: Requesting channel_open for channel 1. 
localhost: Entering interactive session. 
localhost: Sending command: cd /usr/local/loader; java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl 
localhost: Sending command: cd /usr/local/loader; java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl 
localhost: Requesting service exec on channel 1. 
localhost: channel 1: open confirm rwindow 0 rmax 32768 

JAR的输出打印到STDERR,我看到它返回。 9秒后停止,最终通过对等错误重置连接。 STDERR处理程序按预期工作。

我不确定这是Net :: SSH :: Perl处理命令需要花费一段时间才能运行/仅通过STDERR或其他更多的问题。我一直在考虑切换到Net :: SSH2,因为它看起来像一个更全面的特色库,但我真的很想知道为什么这是失败。

SOLUTION

的问题是与输出只会STDERR。我编辑了我的命令以添加2>&1,从而将STDERR重定向到STDOUT,并突然一切按预期工作。

问题在于输出只能进入STDERR。我编辑了我的命令来添加2> & 1,从而将STDERR重定向到STDOUT,并突然一切按预期工作。

my $cmd = "java -Xms4096m -Xmx4096m -DetlDate=20120427 -DmemcacheHosts=host1,host2 -cp etl-0.1-SNAPSHOT.jar com.nnn.platform.service.etl 2>&1"; 

Net :: SSH :: Perl不再被维护,并有一长串已知的未解决的错误。现在可以从CPAN获得更好的模块,如Net::SSH2Net::OpenSSH

例如:

my $ssh = Net::OpenSSH->new($sshost, 
          user => $user, 
          key_path => '/path/to/key.rsa'); 
my ($out, $err) = $ssh->capture2($cmd);