失去与净:: SSH连接:: Perl的(Losing connection with Net::S

2019-09-16 23:02发布

SOLUTION BELOW

我们有中提取数据到一个CSV,它上传到另一台服务器,然后需要连接到其他服务器,并调用Java罐子到CSV加载到内存缓存的ETL系统。 我有一个可以执行的这每一步,但失去了最后一步的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秒后停止,我最终得到通过对错误的连接复位。 标准错误处理程序工作正常。

我不知道这是否与净:: SSH :: Perl的处理是需要一段时间来运行/只在STDERR或更多的东西返回命令的问题。 我一直在考虑改用网:: SSH2,因为它似乎是一个更全面的功能库,但我真的很想知道这是为什么失败。

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

Answer 1:

净:: SSH :: Perl是不再维护,并有已知的bug解决的一个长长的清单。 现在有CPAN有更好的模块的Net :: SSH2或网:: OpenSSH的 。

例如:

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


Answer 2:

问题是与输出只会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";


文章来源: Losing connection with Net::SSH::Perl
标签: perl ssh