溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用Perl和Expect登陸多臺(tái)設(shè)備批量執(zhí)行命令

發(fā)布時(shí)間:2022-10-17 15:28:06 來(lái)源:億速云 閱讀:162 作者:iii 欄目:編程語(yǔ)言

這篇“怎么使用Perl和Expect登陸多臺(tái)設(shè)備批量執(zhí)行命令”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“怎么使用Perl和Expect登陸多臺(tái)設(shè)備批量執(zhí)行命令”文章吧。

實(shí)現(xiàn)如下功能:
  1. 登陸多臺(tái)設(shè)備

設(shè)備登陸信息按如下格式存放于文件中。

$ cat
  1. 在每臺(tái)設(shè)備上批量執(zhí)行命令

要執(zhí)行的命令集合按如下格式存放于文件中。

$ cat cmds.txt
date
w
ifconfig
more
  1. Perl腳本實(shí)現(xiàn),使用了Expect模塊

借助Expect模塊實(shí)現(xiàn)登陸,執(zhí)行命令,捕獲命令回顯,取日志,自動(dòng)回復(fù)more分頁(yè),ping探測(cè)主機(jī)等功能。腳本中的語(yǔ)句形式可供參考。

  1. 腳本如下:

#! /usr/bin/perl

#安裝模塊
#cpan
#install Expect
#install Net::Ping
#perl -MCPAN -e "install autodie"

use utf8;
use Expect;
use autodie;
use Net::Ping;

#0為關(guān)閉本地回顯
#$Expect::Log_Stdout=0;
$ENV{TERM}="xterm";
#不進(jìn)行緩沖直接進(jìn)文件
#$|=1;

#cmds.txt的文件格式:
#一行一條命令
my @cmds;
my $cmds_file="./cmds.txt";
open CMDS,"<",$cmds_file or die "Can't open file $cmds_file: $!\n";
print "commands to run: \n";
while(<CMDS>){
    print "$_";
    chomp;
    push @cmds,$_;
}
close CMDS;
print "=============================\n";

mkdir 'log' unless -e 'log';
chomp(my $now=`date +%y%m%d`);
my $exp=Expect->new;
#$exp->raw_pty(1);

#hosts.txt的文件格式:
#IPv4地址:主機(jī)名:登陸方式(ssh/telnet):用戶名:密碼
my $hosts_file="./hosts.txt";
open HOSTS,"<",$hosts_file or die "Can't open file $hosts_file: $!\n";
while(<HOSTS>){
    chomp;
    @host=split /:/;
    if(&ping_host(@host)){
        &login_host(@host);
    }
}
close HOSTS;
print "Loging finished!\n";

#子程序
sub{
    print "login to $_[1]($_[0])...\n";
    my $user=$_[3];
    my $passwd=$_[4];
    my $ahost=$_[1];
    
    if($_[2] =~ /ssh/i){
        $exp=Expect->spawn("ssh -l $user $_[0]") or die "Can't login to $_[1]($_[0]): $!\n";
        $exp->expect(3,
            [ #使用正則來(lái)表達(dá)包含關(guān)系
                qr/connecting\s\(yes\/no\)\?/i,
                sub {
                        my $self=shift;
                        $self->send("yes\n");
                        exp_continue;
                }
            ],
            [
                qr/password:/i,
                sub {
                        my $self=shift;
                        $self->send("$passwd\n");
                        exp_continue_timeout;
                }
            ]
        );
        #取log
        $exp->log_file("log/$_[1]-$now.log", "w");
        $exp->send("\n"); 
        
        foreach (@cmds){
            $exp->send("$_\n");
            $exp->expect(2,
                [ #使用正則來(lái)表達(dá)包含關(guān)系
                    qr/\[>#$\]/,
                    sub {
                        my $self=shift;
                        $self->send("\n");
                        exp_continue_timeout;
                    }
                ],
                [
                    qr/--More--/i,
                    sub {
                        my $self=shift;
                        $self->send(" ");
                        exp_continue;
                    }
                ]
            );
        }
        #關(guān)閉log
        $exp->log_file(undef);
        #退出登陸
        $exp->send("exit\n") if ($exp->expect(undef,'-re' => '[>#$]')); #undef是癡等
        print "\nLogout from $_[1]($_[0])\n";
    }else{
        $exp=Expect->spawn("telnet $_[0]") or die "Can't login to $_[1]($_[0]): $!\n";
        $exp->expect(30,
            [ #使用正則來(lái)表達(dá)包含關(guān)系,否則就是精確匹配
                qr/$ahost login:/i,
                sub {
                        my $self=shift;
                        $self->send("$user\n");
                        exp_continue;
                }
            ],
            [
                qr/Password:/i,
                sub {
                        my $self=shift;
                        $self->send("$passwd\n");
                        exp_continue_timeout;
                }
            ]
        );
        #取log
        $exp->log_file("log/$_[1]-$now.log", "w");
        $exp->send("\n"); 
        
        foreach (@cmds){
            $exp->send("$_\n");
            $exp->expect(2,
                [ #使用正則來(lái)表達(dá)包含關(guān)系,否則就是精確匹配
                    qr/\[>#$\]/,
                    sub {
                        my $self=shift;
                        $self->send("\n");
                        exp_continue_timeout;
                    }
                ],
                [
                    qr/--More--/i,
                    sub {
                        my $self=shift;
                        $self->send(" ");
                        exp_continue;
                    }
                ]
            );
        }
        #關(guān)閉log
        $exp->log_file(undef);
        #退出登陸
        $exp->send("exit\n") if ($exp->expect(undef,'-re' => '[>#$]')); #undef是癡等
        print "\nLogout from $_[1]($_[0])\n";
    }
}

sub{
    $p=Net::Ping->new("icmp"); #需要root權(quán)限
    if($p->ping($_[0])){
        print "$_[1]($_[0]) is alive\n";
        return 1;
    }else{
        print "$_[1]($_[0]) is die\n";
        return 0;
    }
}

如果沒有root權(quán)限
ping_host子程序可以替換為如下調(diào)用系統(tǒng)ping命令的方法

use Net::Ping::External qw(ping);
...

sub{
    my $alive = ping(host => $_[0]);
    if($alive){
        print "$_[1]($_[0]) is alive\n";
        return 1;
    }else{
        print "$_[1]($_[0]) is die\n";
        return 0;
    }
}

以上就是關(guān)于“怎么使用Perl和Expect登陸多臺(tái)設(shè)備批量執(zhí)行命令”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI