zoukankan      html  css  js  c++  java
  • 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
    ";
    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.0
              inet6 addr: fe80::20c:29ff:fe93:3389/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:90162891 errors:0 dropped:0 overruns:0 frame:0
              TX packets:54468730 errors:0 dropped:0 overruns:0 carrier:0
              collisions: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.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:49500737 errors:0 dropped:0 overruns:0 frame:0
              TX packets:49500737 errors:0 dropped:0 overruns:0 carrier:0
              collisions: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
    ";
    if($stderr){
    print "ErrorCode:$exit
    ";
    print "ErrorMsg:$stderr";
    } else {
    print $stdout;
    }
    exit $exit;
    
    这个/root/auto/a1.sh 需要在192.168.32.79远端机器上,而不是本地机器上
    	
    	
    	


    
       
    
    
  • 相关阅读:
    [FJOI 2016] 神秘数
    [SHOI 2017] 寿司餐厅
    [HAOI 2012] Road
    [HAOI 2012] 容易题
    [TJOI 2018] XOR
    [NOI 2011] 阿狸的打字机
    [ZJOI 2010] 排列计数
    [TJOI2016 & HEOI2016] 字符串
    [HNOI 2011] 数学作业
    【NTT】loj#6261. 一个人的高三楼
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200101.html
Copyright © 2011-2022 走看看