使用某些版本的期望是这样做的公认方式。已经有一个成熟的 OSS 脚本用于与大多数供应商的大多数网络设备进行终端交互。存在同样的问题,但是当供应商更改提示时,更多的眼球正在关注它并更新内容。
查看RANCID。
这是我基于 RANCID 编写的脚本的一个快速示例,用于收集 ASA 上的 ACL 命中计数:
#!/usr/bin/perl
use strict;
use Getopt::Std;
my $usage = "Usage: gethitcnt.pl -c configfile -o outputdir\n\n";
my %opts;
getopts('hc:o:', \%opts);
my $login = sprintf "~%s\/bin\/clogin.in", $ENV{'HOME'};
my $loginrc = sprintf "~%s\/.cloginrc", $ENV{'HOME'};
my $cmdfile = sprintf "~%s\/cmd", $ENV('HOME');
my $date = getdate;
my ($config,$outdir);
unless (-e $login) {
die "Cannot find $login\n\n";
}
unless (-e $loginrc) {
die "Cannot find $loginrc\n\n";
}
if ($opts{h} or !$opts{c}) {
die $usage;
}
if ($opts{o}) {
$outdir = $opts{o};
} else {
$outdir = $ENV{'PWD'};
}
if (-e $opts{c}) {
$config = getconfig($opts{c});
} else {
die "Cannot open config file $opts{c}\n\n";
}
foreach my $firewall (keys %$config) {
foreach my $acl (@{$config->{$firewall}}) {
open (CMD,>$cmdfile);
print CMD "show access-list $acl\n";
print CMD "clear access-list $acl counters\n";
close (CMD);
my $command = ($login,"-x",$cmdfile,$firewall)
open (TMP,"$command |");
my $outfile = sprintf "%s\/%s-%s-%s.txt", $outdir, $firewall, $acl, $date;
open (OUTFILE,>$outfile);
foreach my $line (<TMP>) {
if ($line =~ /\s*(access-list.*\(hitcnt=\d+\))/) {
print OUTFILE "$1\n";
}
}
system ("rm",$cmdfile);
}
}
sub getconfig {
my $configfile = shift;
open(CONFIG,$configfile);
my $out;
foreach (<CONFIG>) {
chomp;
my @$elements = split(/,/);
my $host = shift(@$elements);
$out->{$host} = $elements;
}
close(CONFIG);
return($out);
}
sub getdate {
my @time = localtime;
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $month = $abbr[$time[4]];
my $out = sprintf "%s-%d", $month, $time[3];
return($out);
}