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,並突然一切按預期工作。