安装Net::OpenSSH 库

perl 离线安装 Net::OpenSSH 库

Net::OpenSSH 库 下载地址
https://metacpan.org/pod/Net::OpenSSH
直接获取安装包命令

 

wget https://cpan.metacpan.org/authors/id/S/SA/SALVA/Net-OpenSSH-0.74.tar.gz

编译方式

perl Makefile.PL
make && make install

Net::OpenSSH 库 还需要用到IO:Pty 库,所以我们还需要继续安装 IO:Pty 库

 

IO:Pty 库 下载地址
https://metacpan.org/pod/IO::Pty
直接获取安装包命令

wget  https://cpan.metacpan.org/authors/id/T/TO/TODDR/IO-Tty-1.12.tar.gz

编译方式

perl Makefile.PL
make && make install

测试test.pl 程序

use Net::OpenSSH;                                                                  
use strict;                                                                        
use Encode;                                                                        
use POSIX qw(strftime);                                                            
                                                                                   
my $host="192.168.57.129";                                                         
my $user_name="root";                                                              
my $password="chen";                                                               
                                                                                   
my $ssh = Net::OpenSSH->new($host,user => $user_name,password => $password);       
my @ls = $ssh->capture("ls /");                                                    
$ssh->error and  die "remote ls command failed: " . $ssh->error;                   
print "test:
".@ls."
";                                                          
foreach my $item (@ls) {                                                           
  print "-------".$item."
";                                                      
}                                                                                  
print "========================
";  

运行方法

perl test.pl

这里给大家提一个醒,由于Net::OpenSSH 的库在连接远程服务器时,客户端机器的IP 地址必须要要在 被访问服务器的 ~/.ssh/known_hosts 文件下,否则会报告错误。

错误的信息如下:

remote ls command failed: unable to establish master SSH connection: the authenticity of the target host can't be established; the remote host public key is probably not present on the '~/.ssh/known_hosts' file at test.pl line 13.

详细的解释可以参考官方说明,https://metacpan.org/pod/Net::OpenSSH

Check you can connect to the remote host using the same parameters you are passing to Net::OpenSSH. In particular, ensure that you are running ssh as the same local user.

If you are running your script from a web server, the user would probably be www, apache or something alike.

Common problems are:

Remote host public key not present in known_hosts file.

The SSH protocol uses public keys to identify the remote hosts so that they can not be supplanted by some malicious third parties.

For OpenSSH, usually the server public key is stored in /etc/ssh/ssh_host_dsa_key.pub or in /etc/ssh/ssh_host_rsa_key.pub and that key should be copied into the ~/.ssh/known_hosts file in the local machine (other SSH implementations may use other file locations).

Maintaining the server keys when several hosts and clients are involved may be somewhat inconvenient, so most SSH clients, by default, when a new connection is established to a host whose key is not in the known_hosts file, show the key and ask the user if he wants the key copied there.

Wrong remote host public key in known_hosts file.

This is another common problem that happens when some server is replaced or reinstalled from scratch and its public key changes becoming different to that installed on the known_hosts file.

The easiest way to solve that problem is to remove the old key from the known_hosts file by hand using any editor and then to connect to the server replying yes when asked to save the new key.

Wrong permissions for the ~/.ssh directory or its contents.

OpenSSH client performs several checks on the access permissions of the ~/.ssh directory and its contents and refuses to use them when misconfigured. See the FILES section from the ssh(1) man page.

Incorrect settings for password or public key authentication.

Check that you are using the right password or that the user public key is correctly installed on the server.
原文地址:https://www.cnblogs.com/chenfool/p/7026663.html