簡體   English   中英

使用Net :: SSH :: Perl登錄到遠程計算機

[英]Using Net::SSH::Perl to login to remote machine

我想使用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.

我目前在使用ssh來使用Net :: SSH :: Perl時遇到問題,但是此刻我將此腳本創建為簡單的解決方法。 希望它可以幫助處於類似情況的任何人。 顯然,這些命令或多或少只是連接后發出多個命令的演示。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM