安装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:\n".@ls."\n";                                                          
foreach my $item (@ls) {                                                           
  print "-------".$item."\n";                                                      
}                                                                                  
print "========================\n";  

运行方法

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

可以直接用别人做好的windows OpenSSL 安装包(我用的是0.9.8j版), 可以从 http://www.slproweb.com/products/Win32OpenSSL.html 下载 OpenSSL for Windows,直接安装。 P.S. OpenSSL for Windows 的源代码有一些数据类型和VC6的编译器不兼容,我发现的不兼容的数据类型如下:   在OpenSSL安装目录的下的include/bn.h文件中,将 #define BN_ULLONG unsigned long long #define BN_ULONG unsigned long long #define BN_LONG long long 分别修改为: #define BN_ULLONG ULONGLONG #define BN_ULONG ULONGLONG #define BN_LONG LONGLONG 否则,会出现编译错误。 二、使用OpenSSL 在VC中配置使用以上的函数: 点击菜单:Tools -> Options,弹出对话框"Options",在该对话框中选择"Directories"标签。 在"Show directories for:"的"Include files"选项中新增目录"C:\openssl\include"; "Library files"选择中新增目录"C:\openssl\lib"。 然后在需要链接OpenSSL函数的工程中加入如下两句: #pragma comment(lib, "ssleay32.lib") #pragma comment(lib, "libeay32.lib") 其作用是将OpenSSL所需的导入工程中。 三、问题 我在链接OpenSSL的静态函数时遇到类似以下的问题: Linking... msvcrt.lib(MSVCRT.dll) : error LNK2005: _strchr already defined in libcmtd.lib(strchr.obj) ... 这是由于OpenSSL的静态函数使用的是了VC的多线程DLL的Release版本,而我的程序使用了多线程静态链接的Release版本。 调整OpenSSL的静态函数使用的函数版本即可,调整过程如下: 编辑文件 ms\nt.mak,将该文件第19行 "CFLAG= /MD /Ox /O2 /Ob2 /W3 /WX /Gs0 /GF /Gy /nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -DDSO_WIN32 -D_CRT_SECURE_NO_DEPRECATE - D_CRT_NONSTDC_NO_DEPRECATE /Fdout32 -DOPENSSL_NO_CAMELLIA -DOPENSSL_NO_SEED -DOPENSSL_NO_RC5 -DOPENSSL_NO_MDC2 -DOPENSSL_NO_TLSEXT -DOPENSSL_NO_KRB5 - DOPENSSL_NO_DYNAMIC_ENGINE" 中的"/MD"修改为"/MT"。然后重新编译安装OpenSSL即可。 四、附录:在VC中对C/C++ 运行时不同版本编译指令说明 《在VC中对C/C++ 运行时不同版本编译指令说明》(http://blog.yesky.com/271/dgkang/1737771.shtml)一文中详细介绍了连接不同版本的编译指令如下: C Runtime Library: /MD MSVCRT.LIB 多线程DLL的Release版本 /MDd MSVCRTD.LIB 多线程DLL的Debug版本 /MT LIBCMT.LIB 多线程静态链接的Release版本 /MTd LIBCMTD.LIB 多线程静态链接的Debug版本 /clr MSVCMRT.LIB 托管代码和非托管代码混合 /clr:pure MSVCURT.LIB 纯托管代码 C++ Standard Library: /MD MSVCPRT.LIB 多线程DLL的Release版本 /MDd MSVCPRTD.LIB 多线程DLL的Debug版本 /MT LIBCPMT.LIB 多线程静态链接的Release版本 /MTd LIBCPMTD.LIB 多线程静态链接的Debug版本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值