perl - 与 Net::SSH::Perl 失去连接

标签 perl ssh

下面的解决方案

我们有一个 ETL 系统,将数据提取到 CSV 中,将其上传到另一台服务器,然后需要连接到另一台服务器并调用 java jar 将 csv 加载到内存缓存中。我有一个脚本可以执行这一切的每一步,但在最后一步失去了 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,突然间一切都按预期工作了。

最佳答案

Net::SSH::Perl 不再维护,并且有一长串已知 Unresolved 错误。现在 CPAN 提供了更好的模块,如 Net::SSH2Net::OpenSSH .

例如:

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

关于perl - 与 Net::SSH::Perl 失去连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406736/

相关文章:

ssh - rsync 在管道上通过 SSH 发送时找不到本地目录

java - JSchException : Algorithm negotiation fail

java - JSch addIdentity - 如何传递私钥和公钥字符串与文件路径

git - 错误 : waitpid for C:\Program Files (x86)\PuTTY\plink. exe 失败:没有子进程

git - 无法从 Sourcetree pull Git 远程存储库

perl - 如何仅使用标准 Perl 库发出 HTTP POST 请求?

perl - 如何制作/构建更大的 Selenium 测试套件?

regex - 匹配正则表达式并在单行代码中分配结果

string - perl:显示原始字符串,包括特殊字符

perl - 如何在原型(prototype)检查中作弊?