- #!/usr/bin/perl
- use Net::Telnet::Cisco;
- use DBI;
- use strict;
- my $db=”mysql_db_name”;
- my $dbhost=’localhost’;
- my $user=’mysql_user’;
- my $password=’mysql_pass’;
- my $dbh = DBI->connect(“DBI:mysql:database=$db;host=$dbhost”,
- $user, $password, {RaiseError => 1});
- my $login = “router_login_name”;
- my $pass = “router_password”;
- my $enable = “enable_password”;
- my $host = “localhost_ip”;
- my $routers = {
- ’7507_1_A’ => ‘route_1A_ip’,
- ’7507_1_B’ => ‘route_1B_ip’,
- ’7507_1_C’ => ‘route_1C_ip’,
- ’7507_1_D’ => ‘route_1D_ip’,
- ’7507_2_A’ => ‘route_2A_ip’,
- ’7507_2_B’ => ‘route_2B_ip’,
- ’7507_2_C’ => ‘route_2C_ip’,
- };
- my $routeradd = {
- ’7507_1_A’ => ‘路由器1A’,
- ’7507_1_B’ => ‘路由器1B’,
- ’7507_1_C’ => ‘路由器1C’,
- ’7507_1_D’ => ‘路由器1D’,
- ’7507_2_A’ => ‘路由器2A’,
- ’7507_2_B’ => ‘路由器2B’,
- ’7507_2_C’ => ‘路由器2C’,
- };
- my $session;
- my @output;
- my @output1;
- my @output2;
- my $time=time();
- foreach my $router (keys %{$routers}) {
- $session = Net::Telnet::Cisco->new(-Host => $routers->{$router});
- my $MB = 1024 * 1024;
- $session->max_buffer_length(10 * $MB);
- $session->login(“$login”, “$pass”);
- print “\n\nCheck Router $routeradd->{$router} – $routers->{$router} – “;
- if($session->enable(“$enable”)) {
- # $session->cmd(“terminal length 0″);
- @output = $session->cmd(-String => “show environment all”, -Timeout => 300);
- @output1 = grep (/^RSP\(2\)/,@output);
- @output2 = grep (/^RSP\(3\)/,@output);
- @output1 = split(/[\\t ]+/,$output1[0]);
- @output2 = split(/[\\t ]+/,$output2[0]);
- print \n”;
- # print $output2[0],$output2[1],$output2[2],$output2[3];
- $dbh->do(“INSERT INTO env_temperature VALUES ($time,’$routeradd->{$router}’,'$routers->{$router}’,'$output1[0]‘,’$output1[1]‘,’$output1[2]‘,’$output1[3]‘)”);
- $dbh->do(“INSERT INTO env_temperature VALUES ($time,’$routeradd->{$router}’,'$routers->{$router}’,'$output2[0]‘,’$output2[1]‘,’$output2[2]‘,’$output2[3]‘)”);
- print “INSERT MYSQL \n”;
- } else {
- print – $session->errmsg \n”;
- }
- $session->close;
- }
http://ljnote.com/blog/network/perl-telnet-cisco-temperature.html
阅读(1089) | 评论(0) | 转发(0) |