使用Net :: SSH :: Perl登录远程机器

时间:2014-12-17 15:40:56

标签: perl ssh

我想使用Net :: SSH :: Perl模块对远程机器执行ssh。我可以使用ssh命令从命令行远程登录到机器,但不能从perl脚本远程登录到该机器。任何人都可以帮助我这个

我的代码是:

 $user = 'smetest';$pass = 'smetest';
 print "user is ".$user." password is ".$pass."\n";
 print "Connecting to the Relay host " . $relayteaddress . ".\n";
 $sshrelay = Net::SSH::Perl->new($relayteaddress,  protocol => '2,1', debug => 1);
 print "logging in to Relay $relayteaddress ...\n";
 $sshrelay->login($user, $pass) || die "ssh login didn't work\n";
 print "logged into relay\n";

输出和调试消息:

user is smetest password is smetest
Connecting to the Relay host 192.168.2.175.
W10: Reading configuration data /home/systest/.ssh/config
W10: Reading configuration data /etc/ssh_config
W10: Connecting to 192.168.2.175, port 22.
W10: Remote protocol version 2.0, remote software version OpenSSH_6.4
W10: Net::SSH::Perl Version 1.34, protocol version 2.0.
.10: No compat match: OpenSSH_6.4
W10: Connection established.
logging in to Relay 192.168.2.175 ...
W10: Sent key-exchange init (KEXINIT), wait response.
W10: Algorithms, c->s: 3des-cbc hmac-sha1 none
W10: Algorithms, s->c: 3des-cbc hmac-sha1 none
W10: Entering Diffie-Hellman Group 1 key exchange.
W10: Sent DH public key, waiting for reply.
Key class 'Net::SSH::Perl::Key::RSA' is unsupported: Cannot find current script 'CONDOR_PERF_BAND_ISM2450_80211BGN_CHANNEL_11_ACTIVE_UDP_TX_LGI_AP20' at /usr/share/perl5/FindBin.pm line 205
BEGIN failed--compilation aborted at /usr/share/perl5/FindBin.pm line 205, <GEN26> line 1.
Compilation failed in require at /usr/share/perl5/vendor_perl/Crypt/RSA.pm line 13, <GEN26> line 1.
BEGIN failed--compilation aborted at /usr/share/perl5/vendor_perl/Crypt/RSA.pm line 13, <GEN26> line 1.
Compilation failed in require at /usr/share/perl5/vendor_perl/Net/SSH/Perl/Key/RSA.pm line 14, <GEN26> line 1.
BEGIN failed--compilation aborted at /usr/share/perl5/vendor_perl/Net/SSH/Perl/Key/RSA.pm line 14, <GEN26> line 1.
Compilation failed in require at (eval 45) line 1, <GEN26> line 1.
BEGIN failed--compilation aborted at (eval 45) line 1, <GEN26> line 1.

1 个答案:

答案 0 :(得分:1)

我目前在使用Net :: SSH :: Perl获取ssh时遇到问题,但我现在创建此脚本只是简单的解决方法。希望它可以帮助处于类似情况的任何人。显然,命令或多或少只是连接后发出多个命令的演示。

  use strict;
  use warnings;
  use Expect;

  $exp= Expect->spawn("ssh $host -l $user");

  $exp->expect($timeout,"Password:");
  $exp->send("$pass\r");

  $exp->expect($timeout,-re,'>');
  $exp->send("ls -l\r");

  $exp->expect($timeout,-re,'>');
  $exp->send("mkdir aDir\r");

  $exp->expect($timeout,-re,'>');
  $exp->send("chmod 777 aDir\r");

  $exp->expect($timeout,-re,'>');
  $exp->send("exit\r");