Cisco device management extension command tl-telnet: a large number of cisco devices run the configuration command tl-telnet function in batch: run commands in cmd. list. Tl-telnet reads the host from ip. list sequentially and runs the command in cmd. list. Application Scenario: a large number of cisco devices on www.2cto.com need to run the same command. For example, you can configure a large number of cisco devices or back up configuration files for a large number of cisco devices.
Step 1: Save the tl-telnet.pl code and set executable permissions. Step 2: Create the ip. list and cmd. list Files. Step 3: run # tl-telnet-h ip. list-c cmd. list-l log.txt, where log.txt is an operation log file. Tip: 1. In the ip address. list file, the host name, user name, or password cannot contain the "#" character. It is because "#" is treated as the beginning of the annotation text. 2. the telnet port (23) of all hosts listed in ip. list must be valid; otherwise, the system will automatically stop traversing the following hosts. 3. The list must be valid. Otherwise, the system will automatically stop traversing the following hosts. 4. in the cisco device, you can use exit or quit to exit vty in the enable mode. however, in Net: Telnet: Cisco, you cannot use "exit" or "quit" to exit vty in enable mode. Therefore, in the cmd. list file, the "exit" and "quit" command is not allowed when the command mode is in enable mode. However, you can use "exit" or "quit" in configuration mode ". Www.2cto.com tips: A bug exists in the cmd method operation in Net: Telnet: Cisco. If the command variable entered for the cmd method adopts "$ _", the system will fail. For example, foreach $ cmd (@ cmdList) {$ session-> cmd ($ cmd); // You must define a variable such as $ cmd to specify an array @ cmdList member, do not use "$ _"} $ session-> close; tl-tlnet.pl Code #! /Usr/bin/perl # Version: 0.1 # Date: 2012-04-26 # Author: Darry Guo # Licence: GPL- http://www.fsf.org/licenses/gpl.txt Www.2cto.com use strict; use Getopt: Std; use Net: Telnet: Cisco; sub trim; sub main_sub; sub readfile; sub GetScriptName; sub usage; # use Net :: telnet; my $ script = GetScriptName ($0); my $ script_version = "0.1"; my @ ipList; my @ cmdList; my @ logText; my $ logfile; if (@ ARGV <1) {print "Too few arguments \ n"; usage (); exit 1 ;}my % opt = (); getopts ("Hvh: c: l: ", \ % opt); if ($ opt {H} | $ opt {v}) {usage (); exit (0);} if (( Defined $ opt {h}) & (defined $ opt {c}) & (defined $ opt {l}) {open (IPLIST, "<$ opt {h}") | die ("cocould not open file: $! "); @ IpList = readfile (<IPLIST>); open (CMDLIST," <$ opt {c} ") | die (" cocould not open file: $! "); @ CmdList = readfile (<CMDLIST>); $ logfile = $ opt {l}; main_sub (); close IPLIST; close CMDLIST;} else www.2cto.com {usage (); exit (1 );} ######################################## ############################ sub program ######### ######################################## ################### sub main_sub {# test; # foreach (@ ipList) # {print $ _, "\ n" ;}# print "------------------------ \ n"; # foreach (@ cmdL Ist) # {print $ _, "\ n" ;}# --------------------- my @ hostinfo; system ("/bin/echo> $ logfile"); my $ tmpFile = "temp. log "; foreach (@ ipList) {my $ MB = 1024*1024; @ hostinfo = split (" ", $ _); system ("/bin/echo> $ tmpFile"); my $ session = new Net: Telnet: Cisco (Host => $ hostinfo [0], input_log => $ tmpFile); $ session-> max_buffer_length (5 * $ MB); $ session-> login ($ hostinfo [1], $ hostinfo [2]); $ session-> ena Ble ($ hostinfo [3]); my $ cmd; foreach $ cmd (@ cmdList) {www.2cto.com $ session-> cmd ($ cmd);} $ session-> close; system ("/bin/cat $ tmpFile> $ logfile"); system ("/bin/rm-fr $ tmpFile ");}} sub usage {print <"USAGE"; Usage $ script v $ script_version View Interface status on Cisco devices usage: $ script-h <device list file>-c <command list fil E> [...] options:-h Hostname or IP address list file-c command list file-l log output file-H or-v help reset Copyright 2012 GuoFuSheng This program is free software; you can redistribute it or modifyit under the terms of the GNU General Public License ------------------------------------------------------------------ USAGE} sub tri M {my $ restr =$ _ [0]; $ restr = ~ S/^ + //; $ restr = ~ S/+ $ //; return $ restr;} www.2cto.com sub GetScriptName {my @ str =@_; my $ len = rindex ($ str [0], "/"); return substr ($ str [0], $ len + 1);} sub readfile {my $ line; my $ newstr; my @ newArray; foreach $ line (@_) {chomp $ line; my $ len = length ($ line); if ($ len! = 0) {my $ position = index ($ line, '#'); if ($ position! = 0) {if ($ position =-1) {$ newstr = $ line;} else {$ newstr = substr ($ line, 0, $ position );} push @ newArray, trim ($ newstr) ;}}return @ newArray;} ip. list format # format as flow # ip username pwd1 pwd2 # Note: can't have char '#' in hostname or username or password10.1.1.2 admin 123 abc12310.1.1.3 admin 123 abc123 www.2cto.com 10.1.1.4 admin 123 abc123 cmd. list format # this is cisco commandsh vershow arpshow run # backup config by guofs123