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

标签 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.

最佳答案

我目前在使用 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");

关于perl - 使用 Net::SSH::Perl 登录远程机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27529309/

相关文章:

windows - Perl:检查字符串是否为有效目录,区分大小写

PHP,Perl,问题。这是什么意思 : "$variable -> do some sort of function "

bash - 在使用需要自定义端口号的ssh登录时,有什么方法可以隐藏端口号

bash - 如何使用 ssh 连接到远程服务器并获取信息

java - 通过 JSch 的 SSH 隧道

regex - 我如何跟踪某些元素所属的分组?

perl - 在 Raku 中使用 Perl 5 模块 Data::Printer 的 `show_tied` 选项时,如何关闭它?

bash - SSH 时为新主机自动执行 "yes"

Linux使用 "select: Bad file descriptor"后返回 "cat"错误

image - 如何检测文件是否是perl中的图像