2012-05-02 4 views
3

아래 해결지는 연결 : SSH : 펄

우리가하는 CSV로 데이터를 추출하여 다른 서버에 업로드 한 다음 다른 서버와 통화를 연결해야하는 ETL 시스템을 memcache에 csv를로드하는 자바 jar. 이 모든 단계를 수행 할 수있는 스크립트가 있지만 마지막 단계에서 SSH 연결이 끊어집니다. 원격 시스템의 프로세스가 계속되고 완료됩니다.

Net :: SSH :: Perl을 사용하고 있으며 짧은 시간 동안 실행 한 후 "연결에 실패했습니다 : 연결을 통해 연결이 재설정되었습니다."오류가 발생합니다. 나는이 스크립트를 아래로 비등하고 그 결과를 복제했습니다

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 

항아리의 출력은 다음 STDERR에 출력하고, 나는 그것이 반환을 참조하십시오

#!/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 디버그 정보입니다. 9 초 후 중지하고 결국 피어 오류로 연결을 다시 설정하십시오. STDERR 처리기가 예상대로 작동하고 있습니다.

Net :: SSH :: Perl 처리 명령에 문제가 있는지는 잘 모르겠지만 STDERR 또는 그 이상을 실행하거나 반환하는 데 다소 시간이 걸립니다. 더 많은 기능을 갖춘 라이브러리처럼 보이기 때문에 Net :: SSH2로 전환하는 것을 고려해 보았습니다. 그러나 이것이 왜 실패했는지 알고 싶습니다.

문제

솔루션은 출력은 STDERR로가는이었다. 2>&1을 추가하여 STDERR을 STDOUT으로 리디렉션하고 갑자기 모든 것이 예상대로 작동하도록 내 명령을 편집했습니다.

답변

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"; 
2

Net :: SSH :: Perl은 더 이상 유지 관리되지 않으며 알려진 미해결 버그 목록이 길습니다. 요즘에는 CPAN에서 더 좋은 모듈을 Net::SSH2 또는 Net::OpenSSH으로 사용할 수 있습니다. 예를 들어

:

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