Chinaunix首页 | 论坛 | 博客
  • 博客访问: 570102
  • 博文数量: 201
  • 博客积分: 7734
  • 博客等级: 少将
  • 技术积分: 1994
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-09 19:18
文章分类

全部博文(201)

文章存档

2011年(28)

2010年(173)

分类: Python/Ruby

2011-04-14 17:57:10

  1. #!/usr/bin/perl

  2. use Net::Telnet::Cisco;
  3. use DBI;
  4. use strict;

  5. my $db=”mysql_db_name”;
  6. my $dbhost=’localhost’;
  7. my $user=’mysql_user’;
  8. my $password=’mysql_pass’;
  9. my $dbh = DBI->connect(“DBI:mysql:database=$db;host=$dbhost”,
  10. $user, $password, {RaiseError => 1});

  11. my $login = “router_login_name”;
  12. my $pass = “router_password”;
  13. my $enable = “enable_password”;
  14. my $host = “localhost_ip”;
  15. my $routers = {
  16. ’7507_1_A’ => ‘route_1A_ip’,
  17. ’7507_1_B’ => ‘route_1B_ip’,
  18. ’7507_1_C’ => ‘route_1C_ip’,
  19. ’7507_1_D’ => ‘route_1D_ip’,
  20. ’7507_2_A’ => ‘route_2A_ip’,
  21. ’7507_2_B’ => ‘route_2B_ip’,
  22. ’7507_2_C’ => ‘route_2C_ip’,
  23. };
  24. my $routeradd = {
  25. ’7507_1_A’ => ‘路由器1A’,
  26. ’7507_1_B’ => ‘路由器1B’,
  27. ’7507_1_C’ => ‘路由器1C’,
  28. ’7507_1_D’ => ‘路由器1D’,
  29. ’7507_2_A’ => ‘路由器2A’,
  30. ’7507_2_B’ => ‘路由器2B’,
  31. ’7507_2_C’ => ‘路由器2C’,
  32. };

  33. my $session;
  34. my @output;
  35. my @output1;
  36. my @output2;
  37. my $time=time();

  38. foreach my $router (keys %{$routers}) {
  39. $session = Net::Telnet::Cisco->new(-Host => $routers->{$router});
  40. my $MB = 1024 * 1024;
  41. $session->max_buffer_length(10 * $MB);
  42. $session->login(“$login”, “$pass”);
  43. print “\n\nCheck Router $routeradd->{$router} – $routers->{$router} – “;
  44. if($session->enable(“$enable”)) {
  45. # $session->cmd(“terminal length 0″);
  46. @output = $session->cmd(-String => “show environment all”, -Timeout => 300);
  47. @output1 = grep (/^RSP\(2\)/,@output);
  48. @output2 = grep (/^RSP\(3\)/,@output);
  49. @output1 = split(/[\\t ]+/,$output1[0]);
  50. @output2 = split(/[\\t ]+/,$output2[0]);
  51. print \n”;

  52. # print $output2[0],$output2[1],$output2[2],$output2[3];
  53. $dbh->do(“INSERT INTO env_temperature VALUES ($time,’$routeradd->{$router},'$routers->{$router}’,'$output1[0],’$output1[1],’$output1[2],’$output1[3]));
  54. $dbh->do(“INSERT INTO env_temperature VALUES ($time,’$routeradd->{$router},'$routers->{$router}’,'$output2[0],’$output2[1],’$output2[2],’$output2[3]));
  55. print “INSERT MYSQL \n”;
  56. } else {
  57. print – $session->errmsg \n”;
  58. }
  59. $session->close;
  60. }

 

http://ljnote.com/blog/network/perl-telnet-cisco-temperature.html

阅读(1100) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~