Net::SSH::Perl 模块
生活随笔
收集整理的這篇文章主要介紹了
Net::SSH::Perl 模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<pre name="code" class="python">Net::SSH::Perl - Perl client Interface to SSH Perl SSH的客戶端接口use Net::SSH::Perl;my $ssh = Net::SSH::Perl->new($host);$ssh->login($user, $pass);my($stdout, $stderr, $exit) = $ssh->cmd($cmd);As of version 1.00, Net::SSH::Perl supports both the SSH1 and
SSH2 protocols natively. The two protocols have different
module prerequisitives, so you need to decide which protocol(s)
you plan to use. If you use one or the other, only those modules
for your chosen protocol will be installed; if you choose both,
all of the supporting modules will be installed. Please choose
the protocols you'd like to use from the following list ("Both"
is the default).[1] SSH1[2] SSH2[3] Both SSH1 and SSH2版本1.00, Net::SSH::Perl 支持SSH1和SSH2協議。兩個協議有不用的模塊前提, 因此你需要確定哪些協議 你計劃使用。如果你使用一個或者另一個,只有那些協議你選擇的協議會被安裝默認BOTH
Can't locate Net/SSH/Perl.pm in @INC (you may need to install the Net::SSH::Perl module) (@INC contains: /usr/local/perl/lib/site_perl/5.22.1/x86_64-linux /usr/loca-linux /usr/local/perl/lib/5.22.1 .) at a1.pl line 1.
那就是你的perl沒找到SSH模塊,你可以搜一下你的這個模塊撞到哪里了,然后做個連接過去.find / -name SSH解決:
demo:/root/.cpan/build/Net-SSH-Perl-2.01-czM_pl/blib/lib/Net# cp -R SSH/ /usr/local/perl/lib/site_perl/5.22.1/x86_64-linux/Net/ demo:/root/auto# cat a1.pl
use Net::SSH::Perl;
my $host='192.168.32.79';
my $user='root';
my $passwd='1234567';
my $ssh = Net::SSH::Perl->new($host,port=>22);
$ssh->login($user,$passwd);
my ($stdout,$stderr,$exit) = $ssh->cmd("/sbin/ifconfig -a");
$ssh->cmd("exit");
if($stderr){
print "ErrorCode:$exit\n";
print "ErrorMsg:$stderr";
} else {
print $stdout;
}
exit $exit;
demo:/root/auto# perl a1.pl
eth2 Link encap:Ethernet HWaddr 00:0C:29:93:33:89 inet addr:192.168.32.79 Bcast:192.168.32.255 Mask:255.255.255.0inet6 addr: fe80::20c:29ff:fe93:3389/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:90162891 errors:0 dropped:0 overruns:0 frame:0TX packets:54468730 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:6508620253 (6.0 GiB) TX bytes:4434025169 (4.1 GiB)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING MTU:65536 Metric:1RX packets:49500737 errors:0 dropped:0 overruns:0 frame:0TX packets:49500737 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:48929124401 (45.5 GiB) TX bytes:48929124401 (45.5 GiB)描述:Net::SSH::Perl是一個all-Perl 模塊實現一個SSH客戶端, 兼容SSH-1和SSH-2協議。NET:SSH:Perl 讓你簡單的和安全的執行命令在遠端的機器,接收輸出,錯誤輸出,遠端命令的退出狀態。它包含內置的支持各種認證方法(password 認證,RSA 詢問響應 認證)它完全實現了I/O 緩存,packet傳輸,使用SSH協議的用戶認證,并使用外部的Perl庫 來處理所有數據在不安全的網絡環境下的發送加密它也讀取存在的SSH配置文件。使用Net::SSH::Perl 的一個優勢封裝實現ssh clients節約了處理的開銷:你不需要fork和執行一個單獨的進程來連接一個sshd.根據時間的總量和內存需要來fork一個進程, 這個是相當可觀的,特別是 如果你需要運行在一個持久的Perl環境(mod_perl,比如)派生一個新的進程是一個消耗內部資源的過程。它還簡化了處理使用基于密碼的認證過程,當寫一個封裝ssh你可能需要使用Expect來控制ssh客戶端,然后給它密碼。Net::SSH::Perl 已經內置支持認證協議,因此不在有任何麻煩在任何外部進程通信的問題SSH2協議支持(目前Net::SSH::Perl版本是1.00) 是兼容SSH2 在OPENSSH,也應該完全兼容官方的SSH涉及。如果你找到一個SSH2 涉及不兼容Net::SSH::Perl, 請讓我們知道如果你正在尋找SFTP支持,看一下Net::SFTP, 提供了功能齊全的SFTP實現,SFTP 需要SSH2協議。基本的使用:使用Net:SSH:Perl 是非常簡單的:Net::SSH::Perl->new($host, %params)設置一個新的連接, 調用new方法,連接到$host 和返回一個 Net::SSH::Perl object.new方法接收下面的參數:protocol:協議你希望使用這個連接: 應該是2,1,'1,2' 或者'2,1'第一個說,比較簡單的, "只有使用協議版本(SSH-2或者SSH-1,分別的)后面兩個指定任何一個協議被使用,但是一個協議(在逗號分隔的列表中的第一個)是優先選擇的。對于這個原因,它是安全的使用后面的2個協議,因為它們確保了任何的方式,你可以去連接,如果你的服務器不支持列出的第一個協議,第2個會被使用。(想比你的服務器將至少支持一個協議)默認值是'1,2' 為了兼容OpenSSH,這意味著客戶端會使用SSH-1如果server支持SSH-1,當然,你也可以使用user/global 配置文件cipher指定加密密碼的名字 如果你希望使用這個連接,這個鄙視是一個支持的密碼,在SSH-1, 支持的加密的名字是IDEA,DES,DES3,和Blowfish;在SSH-2,支持的加密是 arcfour, blowfish-cbc, and 3des-cbc.默認的SSH-1 加密是IDEA,SSH-2 加密是3des-cbc這個模塊,遠程執行shell:demo:/root/auto# cat a1.pl
use Net::SSH::Perl;
my $host='192.168.32.79';
my $user='root';
my $passwd='1234567';
my $cmd='sh /root/auto/a1.sh';
my $ssh = Net::SSH::Perl->new($host,port=>22);
$ssh->login($user,$passwd);
my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd");
$ssh->cmd("exit");
print "\$exit is $exit\n";
if($stderr){
print "ErrorCode:$exit\n";
print "ErrorMsg:$stderr";
} else {
print $stdout;
}
exit $exit;這個/root/auto/a1.sh 需要在192.168.32.79遠端機器上,而不是本地機器上
轉載于:https://www.cnblogs.com/zhaoyangjian724/p/6200100.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Net::SSH::Perl 模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xamarin 学习笔记02- IOS
- 下一篇: 大量POI点展示的一种解决方案