Chinaunix首页 | 论坛 | 博客
  • 博客访问: 182198
  • 博文数量: 16
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 753
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 10:57
文章分类

全部博文(16)

文章存档

2017年(3)

2016年(2)

2015年(3)

2014年(1)

2013年(2)

2012年(5)

我的朋友

分类: 网络与安全

2012-04-26 14:55:20

tl-telnet

功能:
    批量运行cmd.list中的命令。
    tl-telnet依次从ip.list读取主机,并运行cmd.list中的命令。

应用场景:
    大量cisco设备中需要运行相同的命令。
    如配置大量的cisco设备,或备份大量的cisco设备的配置文件等等

使用办法
    step1:将tl-telnet.pl代码保存并设置可执行权限。
    step2:建立ip.list、cmd.list文件。
    step3:运行
          #tl-telnet -h ip.list -c cmd.list -l log.txt
          其中log.txt是操作日志文件。


提示:
1.在ip.list文件中,主机名称或用户名称或密码中不能存在"#"字符。
  是由于"#"被当作注解文字的开始。
2.在ip.list中列出的所有主机的telnet端口(23)必须有效,否则系统会自动中止遍历下面的主机。
3.在ip.list中列出必须有效,否则系统会自动中止遍历下面的主机。
4.在cisco设备中,在使能模式(#)下可以使用“exit”或“quit”退出vty.
  但在Net::Telnet::Cisco中,禁止在使能模式(#)下可以使用“exit”或“quit”退出vty。
  所以在cmd.list文件中,当命令模式位于使能模式时禁止使用“exit”“quit”命令。
  但在配置模式下可以使用“exit”或“quit”。

 

重要提示:
    在Net::Telnet::Cisco中的cmd方法操作中有bug存在,若输入给cmd方法的命令变量采用“$_”时,系统会出错。
    如:
      foreach $cmd(@cmdList)           
      {
          $session->cmd($cmd);       //必须定义一个变量如$cmd来给转定数组@cmdList成员,禁止采用“$_”
      }
      $session->close;

tl-tlnet.pl代码

 


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. # Version : 0.1
  3. # Date : 2012-04-26
  4. # Licence : GPL - http://www.fsf.org/licenses/gpl.txt

  5. use strict;
  6. use Getopt::Std;
  7. use Net::Telnet::Cisco;
  8. sub trim;
  9. sub main_sub;
  10. sub readfile;
  11. sub GetScriptName;
  12. sub usage;
  13. #use Net::Telnet;

  14. my $script = GetScriptName($0);
  15. my $script_version = "0.1";
  16. my @ipList;
  17. my @cmdList;
  18. my @logText;
  19. my $logfile;

  20. if (@ARGV < 1) {
  21.      print "Too few arguments\n";
  22.      usage();
  23.      exit 1;
  24. }

  25. my %opt=();
  26. getopts("Hvh:c:l:",\%opt);

  27. if($opt{H} || $opt{v})
  28. {
  29.     usage();
  30.     exit(0);
  31. }

  32. if ( (defined $opt{h}) && (defined $opt{c}) && (defined $opt{l}) )
  33. {
  34.     open (IPLIST, "<$opt{h}") || die ("Could not open file : $!");
  35.     @ipList = readfile(<IPLIST>);
  36.     open (CMDLIST, "<$opt{c}") || die ("Could not open file : $!");
  37.     @cmdList = readfile(<CMDLIST>);
  38.     $logfile = $opt{l};

  39.     main_sub();

  40.     close IPLIST;
  41.     close CMDLIST;
  42. }
  43. else
  44. {
  45.     usage();
  46.     exit(1);

  47. }


  48. ####################################################################
  49. # sub program #
  50. ####################################################################
  51. sub main_sub
  52. {
  53.    #test;
  54.    #foreach (@ipList)
  55.    #{ print $_,"\n";}

  56.    #print "--------------------------\n";

  57.    #foreach (@cmdList)
  58.    #{ print $_,"\n";}

  59.    #-----------------------
  60.    my @hostinfo;
  61.    system("/bin/echo > $logfile");
  62.    my $tmpFile = "temp.log";
  63.    foreach (@ipList)
  64.    {
  65.       my $MB = 1024 * 1024;
  66.       @hostinfo = split(" ",$_);
  67.       system("/bin/echo > $tmpFile");
  68.       my $session = new Net::Telnet::Cisco(Host => $hostinfo[0], Input_log => $tmpFile);
  69.       $session->max_buffer_length(5 * $MB);
  70.       $session->login($hostinfo[1],$hostinfo[2]);
  71.       $session->enable($hostinfo[3]);
  72.       my $cmd;
  73.       foreach $cmd(@cmdList)
  74.       {
  75.           $session->cmd($cmd);
  76.       }
  77.       $session->close;
  78.       system("/bin/cat $tmpFile >> $logfile");
  79.       system("/bin/rm -fr $tmpFile");
  80.    }
  81. }



  82. sub usage {
  83.     print << "USAGE";
  84. --------------------------------------------------------------------
  85. $script v$script_version

  86. View Interface status on Cisco devices

  87. Usage: $script -h <device list file> -c <command list file> [...]
  88. Options: -h Hostname or IP address list file
  89.                  -c command list file
  90.                  -l log output file
  91.                  -H or -v help

  92. --------------------------------------------------------------------
  93.  

  94. This program is free software; you can redistribute it or modify
  95. it under the terms of the GNU General Public License
  96. --------------------------------------------------------------------

  97. USAGE
  98. }

  99. sub trim
  100. {
  101.   my $restr = $_[0];
  102.   $restr=~s/^ +//;
  103.   $restr=~s/ +$//;
  104.   return $restr;
  105. }


  106. sub GetScriptName
  107. {
  108.    my @str = @_;
  109.    my $len = rindex($str[0],"/");
  110.    return substr($str[0],$len + 1);
  111. }

  112. sub readfile
  113. {
  114.    my $line;
  115.    my $newstr;
  116.    my @newArray;
  117.    foreach $line(@_)
  118.    {
  119.      chomp $line;
  120.      my $len = length($line);
  121.      if ($len != 0)
  122.      {
  123.         my $position = index($line,'#');
  124.         if ($position != 0)
  125.         {
  126.             if ($position == -1)
  127.             {
  128.                $newstr = $line;
  129.             }
  130.             else
  131.             {
  132.                $newstr = substr($line,0,$position);
  133.             }
  134.             push @newArray , trim($newstr);
  135.         }
  136.      }
  137.    }
  138.    return @newArray;
  139. }


 

ip.list格式

# format as flow
#      ip username pwd1  pwd2
#Note: can't have char '#' in hostname or username or password
10.1.1.2 admin 123 abc123
10.1.1.3 admin 123 abc123
10.1.1.4 admin 123 abc123


cmd.list格式

#this is cisco command
sh ver
show arp
show run   #backup config

 

 


 

 

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