cpan安装Net::SSH::Perl中遇到的一些问题

2019-10-01 13:36:54于海丽

实例

实例1: 登录一台远程主机,完成简单操作。


use strict;
        use warnings;
        use Net::SSH::Perl;
        my $host = "192.168.14.222";
        my $user = "root";
        my $passwd = "123456";
        my %params = (
        protocal => '2,1',
        debug => '1',
        );
        my $ssh = Net::SSH::Perl->new($host,%params);
        $ssh->login($user,$passwd);
        my ($out,$err,$exit) = $ssh->cmd("ifconfig");
        print "$outn";
        $ssh->cmd("mkdir /home/user;touch /home/user/{1..10}.log");

实例2:通过一个远程主机列表,登录N台远程主机,完成可提问式的互动操作。
远程主机列表文件Remoto_host_list.txt文件:
192.168.14.222 root 123456
192.168.14.223 root 123456

程序:


use strict;
        use warnings;
        use Net::SSH::Perl;
        open HOST_LIST, "Remote_host_list.txt" or die "can`t open this filen";
        sub MySSH{
        my ($host,$user,$passwd) = split;
        my %params = (
        protocal => '2,1',
        # debug => '1',
        );
        my $ssh = Net::SSH::Perl->new($host,%params);
        $ssh->login($user,$passwd);
        $ssh->register_handler("stdout",sub{
            my($channel,$buffer) = @_;
            my $str = $buffer->bytes;
            if($str =~ /(.*)[Y/n/?]/i){
                  $channel->send_data("yn")
              }}
        );
        $ssh->cmd("aptitude install ppp");
        print "n** $host complete...nn";
        };