>[оверквотинг удален]
>>perl expect site:opennet.ru/openforum/
>>perl telnet site:opennet.ru/openforum/
>>; do
>> google.ru + $S$ + ENTER
>>done
>>
>
>ок, понял не дурак, был бы дурак не понял.
>просто у многих есть уже наработки хотелось посмотреть как это у других
>#!/usr/bin/perl -w
use strict;
use Net::Telnet;
my $script_prefix = '/usr/local/scripts/cisco';
my $r_db = "$script_prefix/routers.list";
my $cconfig = "$script_prefix/.config";
my $cexec = "$script_prefix/.exec";
my $readline = '';
my $WriteMem = 1; # [0,1] - [NoWrite,Write]
my $IOSmode = 1; # [0,1] - [Exec,Config]
#No work
my $StepByStepMode = 0;
#-
my $LoopExecMode = 0;
my $CountLoop = 24*60*60; # 24-hours
my $WriteLog = undef;
#my $WriteLog = "$script_prefix/CiscoTelnetMng.log";
#-
if ( !open(ROUTERS, "<$r_db") ) {
print "Error open file\n";
exit;
}
while(<ROUTERS>) {
$readline = $_;
if ( not ($readline =~ /^#/) and not ($readline =~ /^\s*$/) ) {
if ($readline =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[\w.-]+)\s+(\S+)\s+(\S+)/ ) {
my $router_ip = $1;
my $login = $2;
my $password = $3;
my $prompt = '';
my @OutLines = ();
my $telnet_h = undef;
my ($prematch, $match);
print "Connecting to router $router_ip\n";
eval {
$telnet_h = new Net::Telnet(Host => "$router_ip", Timeout => 30, Dump_Log => "$script_prefix/save_conf_dump.log");
#$telnet_h = new Net::Telnet(Host => "$router_ip", Timeout => 30);
};
if ( $@ ) {
chop($@);
printf "Error connecting: (%s) \n", $@;
next;
} else {
print "Connect success!\n";
}
$telnet_h->max_buffer_length(500000);
($prematch, $match) = $telnet_h->waitfor('/Username:.*$|login:.*$/i');
print "$match";
$telnet_h->print($login);
print "$login\n";
($prematch, $match) = $telnet_h->waitfor('/password:.*$/i');
print "$match";
$telnet_h->print($password);
$password =~ s/./*/g;
print "$password\n";
$telnet_h->print('terminal length 0');
print "$prompt#terminal length 0\n";
$telnet_h->waitfor("/$prompt/");
$telnet_h->print('terminal width 512');
print "$prompt#terminal width 512\n";
$telnet_h->waitfor("/$prompt/");
if ($IOSmode) {
$telnet_h->cmd(String => "configure terminal", Prompt => "/$prompt/");
print "$prompt>configure terminal\n";
if ( !open(CONF, "<$cconfig") ) {
print "Error open cisco config file\n";
print "Disconnect\n";
$telnet_h->close;
exit;
}
while(<CONF>) {
my $conf_line = $_;
print "$prompt(config)#$_";
$conf_line =~ s/\n|\r//g;
@OutLines = $telnet_h->cmd(String => "$conf_line", Prompt => "/$prompt/");
#foreach (@OutLines) {
# $_ =~ s/\n|\r//g;
# print ":$_:\n";
#}
}
close(CONF);
$telnet_h->cmd(String => "\cZ", Prompt => "/$prompt#/");
if ($WriteMem) {
$telnet_h->cmd(String => "write memory", Prompt => "/$prompt/");
$telnet_h->waitfor("/$prompt/");
print "\n$prompt#write memory\n";
}
} else {
if ( !open(CONF, "<$cexec") ) {
print "Error open cisco exec file\n";
print "Disconnect\n";
$telnet_h->close;
exit;
}
while(<CONF>) {
my $exec_line = $_;
if ($LoopExecMode) {
if (defined($WriteLog)) {
if ( !open(LOG, ">$WriteLog") ) {
print "Error open log file!\n";
$telnet_h->close;
exit;
}
select(LOG);
$| = 1;
}
#скобки в Prompt экранируются двойными слешами..
for my $cnt (1 .. $CountLoop) {
my @OutLines = $telnet_h->cmd(String => "$exec_line", Prompt => "/$prompt/");
print "\e[H\e[2J" if (not defined($WriteLog));
if (@OutLines) {
foreach (@OutLines) {
$_ =~ s/\n|\r//g;
if (defined($WriteLog)) {
print LOG "$_\n" if ($_ ne '');
} else {
print "$_\n" if ($_ ne '');
}
}
}
sleep(1);
}
close(LOG) if (defined($WriteLog));
} else {
@OutLines = $telnet_h->cmd(String => "$exec_line", Prompt => "/$prompt/");
foreach (@OutLines) {
$_ =~ s/\n|\r//g;
print "$_\n";
}
}
}
close(CONF);
}
print "Disconnect\n";
$telnet_h->close;
}
}
}
close(ROUTERS);
exit;
Очень давно делал. Код уже плесенью покрылся :)