3

下面的解决方案

我们有一个 ETL 系统,它将数据提取到 CSV 中,将其上传到另一台服务器,然后需要连接到另一台服务器并调用 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,因为它似乎是一个功能更齐全的库,但我真的很想知道为什么会失败。

解决方案

问题在于输出只到 STDERR。我编辑了我的命令以添加2>&1并因此将 STDERR 重定向到 STDOUT,突然一切都按预期工作。

4

2 回答 2

2

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);
于 2012-05-03T08:01:06.343 回答
0

问题在于输出只到 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";
于 2012-05-02T14:44:05.910 回答