Net::SSH::Perl 模块
生活随笔
收集整理的這篇文章主要介紹了
Net::SSH::Perl 模块
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<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協(xié)議。兩個(gè)協(xié)議有不用的模塊前提, 因此你需要確定哪些協(xié)議 你計(jì)劃使用。如果你使用一個(gè)或者另一個(gè),只有那些協(xié)議你選擇的協(xié)議會(huì)被安裝默認(rèn)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沒(méi)找到SSH模塊,你可以搜一下你的這個(gè)模塊撞到哪里了,然后做個(gè)連接過(guò)去.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是一個(gè)all-Perl 模塊實(shí)現(xiàn)一個(gè)SSH客戶端, 兼容SSH-1和SSH-2協(xié)議。NET:SSH:Perl 讓你簡(jiǎn)單的和安全的執(zhí)行命令在遠(yuǎn)端的機(jī)器,接收輸出,錯(cuò)誤輸出,遠(yuǎn)端命令的退出狀態(tài)。它包含內(nèi)置的支持各種認(rèn)證方法(password 認(rèn)證,RSA 詢問(wèn)響應(yīng) 認(rèn)證)它完全實(shí)現(xiàn)了I/O 緩存,packet傳輸,使用SSH協(xié)議的用戶認(rèn)證,并使用外部的Perl庫(kù) 來(lái)處理所有數(shù)據(jù)在不安全的網(wǎng)絡(luò)環(huán)境下的發(fā)送加密它也讀取存在的SSH配置文件。使用Net::SSH::Perl 的一個(gè)優(yōu)勢(shì)封裝實(shí)現(xiàn)ssh clients節(jié)約了處理的開(kāi)銷:你不需要fork和執(zhí)行一個(gè)單獨(dú)的進(jìn)程來(lái)連接一個(gè)sshd.根據(jù)時(shí)間的總量和內(nèi)存需要來(lái)fork一個(gè)進(jìn)程, 這個(gè)是相當(dāng)可觀的,特別是 如果你需要運(yùn)行在一個(gè)持久的Perl環(huán)境(mod_perl,比如)派生一個(gè)新的進(jìn)程是一個(gè)消耗內(nèi)部資源的過(guò)程。它還簡(jiǎn)化了處理使用基于密碼的認(rèn)證過(guò)程,當(dāng)寫(xiě)一個(gè)封裝ssh你可能需要使用Expect來(lái)控制ssh客戶端,然后給它密碼。Net::SSH::Perl 已經(jīng)內(nèi)置支持認(rèn)證協(xié)議,因此不在有任何麻煩在任何外部進(jìn)程通信的問(wèn)題SSH2協(xié)議支持(目前Net::SSH::Perl版本是1.00) 是兼容SSH2 在OPENSSH,也應(yīng)該完全兼容官方的SSH涉及。如果你找到一個(gè)SSH2 涉及不兼容Net::SSH::Perl, 請(qǐng)讓我們知道如果你正在尋找SFTP支持,看一下Net::SFTP, 提供了功能齊全的SFTP實(shí)現(xiàn),SFTP 需要SSH2協(xié)議。基本的使用:使用Net:SSH:Perl 是非常簡(jiǎn)單的:Net::SSH::Perl->new($host, %params)設(shè)置一個(gè)新的連接, 調(diào)用new方法,連接到$host 和返回一個(gè) Net::SSH::Perl object.new方法接收下面的參數(shù):protocol:協(xié)議你希望使用這個(gè)連接: 應(yīng)該是2,1,'1,2' 或者'2,1'第一個(gè)說(shuō),比較簡(jiǎn)單的, "只有使用協(xié)議版本(SSH-2或者SSH-1,分別的)后面兩個(gè)指定任何一個(gè)協(xié)議被使用,但是一個(gè)協(xié)議(在逗號(hào)分隔的列表中的第一個(gè))是優(yōu)先選擇的。對(duì)于這個(gè)原因,它是安全的使用后面的2個(gè)協(xié)議,因?yàn)樗鼈兇_保了任何的方式,你可以去連接,如果你的服務(wù)器不支持列出的第一個(gè)協(xié)議,第2個(gè)會(huì)被使用。(想比你的服務(wù)器將至少支持一個(gè)協(xié)議)默認(rèn)值是'1,2' 為了兼容OpenSSH,這意味著客戶端會(huì)使用SSH-1如果server支持SSH-1,當(dāng)然,你也可以使用user/global 配置文件cipher指定加密密碼的名字 如果你希望使用這個(gè)連接,這個(gè)鄙視是一個(gè)支持的密碼,在SSH-1, 支持的加密的名字是IDEA,DES,DES3,和Blowfish;在SSH-2,支持的加密是 arcfour, blowfish-cbc, and 3des-cbc.默認(rèn)的SSH-1 加密是IDEA,SSH-2 加密是3des-cbc這個(gè)模塊,遠(yuǎn)程執(zhí)行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;這個(gè)/root/auto/a1.sh 需要在192.168.32.79遠(yuǎn)端機(jī)器上,而不是本地機(jī)器上
轉(zhuǎn)載于:https://www.cnblogs.com/zhaoyangjian724/p/6200100.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Net::SSH::Perl 模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: xamarin 学习笔记02- IOS
- 下一篇: 大量POI点展示的一种解决方案