Difference between revisions of "Net: : Telnet: : Cisco"
From Teknologisk videncenter
m (New page: [http://search.cpan.org/dist/Net-Telnet-Cisco/ '''Net::Telnet::Cisco'''] er et Perl modul som er beregnet til at koble op imod Cisco udstyr. == Eksempel 1 == <source lang=perl> #!...) |
m |
||
Line 37: | Line 37: | ||
} | } | ||
} | } | ||
+ | </source> | ||
+ | == Eksempel 2 == | ||
+ | <source lang=cli> | ||
+ | #!/usr/bin/perl -w | ||
+ | use strict; | ||
+ | |||
+ | use Net::Ping; | ||
+ | use Net::Telnet::Cisco; | ||
+ | |||
+ | my @output; | ||
+ | my $logfile = 'C:\temp\logfile'; | ||
+ | my $session; # Til at holde Net::Telnet::Cisco instans | ||
+ | my $backup_host = "201.1.1.201"; | ||
+ | ######## Status info | ||
+ | my $ciscoConfReg; | ||
+ | my $ciscoIOS; | ||
+ | my $ciscoIOSfile; | ||
+ | my $ciscoFlashSize; | ||
+ | my $ciscoMemSize; | ||
+ | my $ciscoModel; | ||
+ | ##################### | ||
+ | # | ||
+ | # INIT | ||
+ | |||
+ | if ( ! open EUC_LOG, ">> $logfile" ) { | ||
+ | die "Kan ikke åbne logfil"; | ||
+ | } else { | ||
+ | info(101, "Running.\n"); | ||
+ | } | ||
+ | |||
+ | my @devices = ( "192.168.22.50","80.80.12.112","192.168.22.51","marswan" ); | ||
+ | #system "cls"; | ||
+ | foreach (@devices) { | ||
+ | |||
+ | #print "Session oprettes til $_\n"; | ||
+ | if ( icmpOnline($_) ) { | ||
+ | print "$_ OK\n"; | ||
+ | } else { | ||
+ | print "$_ offline....\n"; | ||
+ | warning( 101, "Pingtest: $_ offline"); | ||
+ | } | ||
+ | } | ||
+ | if ( ciscoLogin('192.168.22.50', 'cisco') ) { | ||
+ | print "session: $session\n"; | ||
+ | ciscoGetInfo(); | ||
+ | ciscoClose(); | ||
+ | print "Model: $ciscoModel\nConfReg: $ciscoConfReg\nMemory: $ciscoMemSize MB\n Flash: $ciscoFlashSize MB\nIOS: $ciscoIOS\nFile: $ciscoIOSfile\n"; | ||
+ | |||
+ | } else { | ||
+ | print "Der kunne ikke logges ind\n"; | ||
+ | warning( 102, "Der kunne ikke logges ind på host"); | ||
+ | } | ||
+ | |||
+ | sub icmpOnline { | ||
+ | my $ping_instance; | ||
+ | my $host = shift; | ||
+ | $ping_instance = Net::Ping->new('icmp' , 1); | ||
+ | if ($ping_instance->ping($host) ) { | ||
+ | return 1; | ||
+ | } else { | ||
+ | return 0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | sub ciscoCommand { | ||
+ | my $command=shift; | ||
+ | my @result; | ||
+ | |||
+ | @result = $session->cmd($command); | ||
+ | return @result; | ||
+ | } | ||
+ | |||
+ | |||
+ | sub ciscoLogin { | ||
+ | my $host = shift; | ||
+ | my $password = shift; | ||
+ | $session = Net::Telnet::Cisco->new(Host => $host, | ||
+ | Input_log => "C:\\temp\\input-log.$host", | ||
+ | Timeout => 60, | ||
+ | Errmode => "return"); | ||
+ | if ( ! $session->login('login', $password ) ) { | ||
+ | warn ("ADVARSEL: Der kunne ikke skabes kontakt til: $host\n"); | ||
+ | return 0; | ||
+ | } else { | ||
+ | # Enable mode | ||
+ | if ($session->enable( $password ) ) { | ||
+ | #@output = $session->cmd("configure terminal\n"); | ||
+ | $session->cmd("terminal lenght 0"); | ||
+ | } else { | ||
+ | warn "Can't enable: " . $session->errmsg; | ||
+ | return 0; | ||
+ | } | ||
+ | return 1; # Success | ||
+ | } | ||
+ | } | ||
+ | |||
+ | sub ciscoClose { | ||
+ | if ( !$session->close ) { | ||
+ | warning( 103, "Kan ikke logge af: "); | ||
+ | } | ||
+ | } | ||
+ | sub ciscoGetInfo { | ||
+ | my @output = ciscoCommand('show version'); | ||
+ | foreach (@output) { | ||
+ | if ( $_ =~ m/\S+\s+(\S+).+\(revision.+/ ) { | ||
+ | $ciscoModel = $1; | ||
+ | } | ||
+ | if ( $_ =~ m/Configuration register is (\S+)/ ) { | ||
+ | $ciscoConfReg = $1; | ||
+ | } | ||
+ | if ( $_ =~ m/\(revision \S+\s+\S+\s+(\d+)K\/(\d+)K/ ) { | ||
+ | $ciscoMemSize = int(($1+$2)/1024); | ||
+ | } | ||
+ | if ( $_ =~ m/(\d+)K.*flash/ ) { | ||
+ | $ciscoFlashSize = int($1/1024); | ||
+ | } | ||
+ | if ( $_ =~ m/(IOS.*)/ ) { | ||
+ | $ciscoIOS = $1; | ||
+ | } | ||
+ | if ( $_ =~ m/System image file is ".*:(.*)"/ ) { | ||
+ | $ciscoIOSfile = $1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | sub info { | ||
+ | my($arg1, $arg2) = @_; | ||
+ | print EUC_LOG "INFO...: ", tid(), " nummer: ", $arg1, " -> ", $arg2, "\n"; | ||
+ | print "INFO: ", $arg2, "\n"; | ||
+ | } | ||
+ | |||
+ | sub warning { | ||
+ | my $arg1 = shift; | ||
+ | my $arg2 = shift; | ||
+ | print EUC_LOG "WARNING: ", tid(), " nummer: ", $arg1, " -> ", $arg2, "\n"; | ||
+ | print "WARNING: ", $_[1],"\n"; | ||
+ | } | ||
+ | |||
+ | sub error { | ||
+ | my $arg1 = shift; | ||
+ | my $arg2 = shift; | ||
+ | print EUC_LOG "ERROR..: ", tid(), " nummer: ", $arg1, " -> ", $arg2, "\n"; | ||
+ | print "ERROR: ", $arg2, "\n"; | ||
+ | } | ||
+ | |||
+ | sub tid { | ||
+ | my @mon_abbr = | ||
+ | qw( Jan Feb Mar Apr Maj Jun Jul Aug Sep Okt Nov Dec ); | ||
+ | |||
+ | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); | ||
+ | |||
+ | return sprintf " %02i:%02i:%02i %2i. %s %4i ", $hour, $min, $sec, | ||
+ | $mday, $mon_abbr[$mon], $year+1900; | ||
+ | } | ||
</source> | </source> | ||
[[Category:Perl]] | [[Category:Perl]] |
Revision as of 12:27, 30 September 2009
Net::Telnet::Cisco er et Perl modul som er beregnet til at koble op imod Cisco udstyr.
Eksempel 1
#!/usr/bin/env perl
use Net::Telnet::Cisco;
my $backup_host = "201.1.1.201";
my @devices = ( "H1R1", "H1R2", "H1R3", "H2R1", "H2R2", "H2R3", "H3R1", "H3R2",
"H3R3", "H4R1", "H4R2", "H4R3", "H1S", "H2S", "H3S", "H4S", "BGPS" );
foreach (@devices) {
print "Session oprettes til $_\n";
my $session = Net::Telnet::Cisco->new(Host => $_,
Input_log => "/tmp/input-log.$_",
Timeout => 60);
print "der logges ind paa $_\n";
#Login via Telnet med passwordet cisco
if ( ! $session->login('login', 'cisco') ) {
print ("ADVARSEL: Der kunne ikke skabes kontakt til: $_\n");
} else {
# Enable mode (Password cisco)
if ($session->enable("cisco") ) {
@output = $session->cmd("copy running-config tftp:\n$backup_host\n$_-config\n");
print "@output\n"; # Vis på skærmen hvad der sker
} else {
warn "Can't enable: " . $session->errmsg;
}
$session->close;
}
}
Eksempel 2
#!/usr/bin/perl -w
use strict;
use Net::Ping;
use Net::Telnet::Cisco;
my @output;
my $logfile = 'C:\temp\logfile';
my $session; # Til at holde Net::Telnet::Cisco instans
my $backup_host = "201.1.1.201";
######## Status info
my $ciscoConfReg;
my $ciscoIOS;
my $ciscoIOSfile;
my $ciscoFlashSize;
my $ciscoMemSize;
my $ciscoModel;
#####################
#
# INIT
if ( ! open EUC_LOG, ">> $logfile" ) {
die "Kan ikke åbne logfil";
} else {
info(101, "Running.\n");
}
my @devices = ( "192.168.22.50","80.80.12.112","192.168.22.51","marswan" );
#system "cls";
foreach (@devices) {
#print "Session oprettes til $_\n";
if ( icmpOnline($_) ) {
print "$_ OK\n";
} else {
print "$_ offline....\n";
warning( 101, "Pingtest: $_ offline");
}
}
if ( ciscoLogin('192.168.22.50', 'cisco') ) {
print "session: $session\n";
ciscoGetInfo();
ciscoClose();
print "Model: $ciscoModel\nConfReg: $ciscoConfReg\nMemory: $ciscoMemSize MB\n Flash: $ciscoFlashSize MB\nIOS: $ciscoIOS\nFile: $ciscoIOSfile\n";
} else {
print "Der kunne ikke logges ind\n";
warning( 102, "Der kunne ikke logges ind på host");
}
sub icmpOnline {
my $ping_instance;
my $host = shift;
$ping_instance = Net::Ping->new('icmp' , 1);
if ($ping_instance->ping($host) ) {
return 1;
} else {
return 0;
}
}
sub ciscoCommand {
my $command=shift;
my @result;
@result = $session->cmd($command);
return @result;
}
sub ciscoLogin {
my $host = shift;
my $password = shift;
$session = Net::Telnet::Cisco->new(Host => $host,
Input_log => "C:\\temp\\input-log.$host",
Timeout => 60,
Errmode => "return");
if ( ! $session->login('login', $password ) ) {
warn ("ADVARSEL: Der kunne ikke skabes kontakt til: $host\n");
return 0;
} else {
# Enable mode
if ($session->enable( $password ) ) {
#@output = $session->cmd("configure terminal\n");
$session->cmd("terminal lenght 0");
} else {
warn "Can't enable: " . $session->errmsg;
return 0;
}
return 1; # Success
}
}
sub ciscoClose {
if ( !$session->close ) {
warning( 103, "Kan ikke logge af: ");
}
}
sub ciscoGetInfo {
my @output = ciscoCommand('show version');
foreach (@output) {
if ( $_ =~ m/\S+\s+(\S+).+\(revision.+/ ) {
$ciscoModel = $1;
}
if ( $_ =~ m/Configuration register is (\S+)/ ) {
$ciscoConfReg = $1;
}
if ( $_ =~ m/\(revision \S+\s+\S+\s+(\d+)K\/(\d+)K/ ) {
$ciscoMemSize = int(($1+$2)/1024);
}
if ( $_ =~ m/(\d+)K.*flash/ ) {
$ciscoFlashSize = int($1/1024);
}
if ( $_ =~ m/(IOS.*)/ ) {
$ciscoIOS = $1;
}
if ( $_ =~ m/System image file is ".*:(.*)"/ ) {
$ciscoIOSfile = $1;
}
}
}
sub info {
my($arg1, $arg2) = @_;
print EUC_LOG "INFO...: ", tid(), " nummer: ", $arg1, " -> ", $arg2, "\n";
print "INFO: ", $arg2, "\n";
}
sub warning {
my $arg1 = shift;
my $arg2 = shift;
print EUC_LOG "WARNING: ", tid(), " nummer: ", $arg1, " -> ", $arg2, "\n";
print "WARNING: ", $_[1],"\n";
}
sub error {
my $arg1 = shift;
my $arg2 = shift;
print EUC_LOG "ERROR..: ", tid(), " nummer: ", $arg1, " -> ", $arg2, "\n";
print "ERROR: ", $arg2, "\n";
}
sub tid {
my @mon_abbr =
qw( Jan Feb Mar Apr Maj Jun Jul Aug Sep Okt Nov Dec );
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
return sprintf " %02i:%02i:%02i %2i. %s %4i ", $hour, $min, $sec,
$mday, $mon_abbr[$mon], $year+1900;
}