amit_singh has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I got the below snippet of perl script(cisco.pl) which will telnet into cisco router with the defined username & password in the script.The script is defined to execute the command "sh ip bgp $prefix ". The argument will be taken from a file specified "INPUT.txt" & output will be dumped to another file "OUTPUT.txt" as below.
#./cisco.pl INPUT.txt OUTPUT.txt
The INPUT.txt will be having the all the below IP prefixes(4.21.103.0/24,10.10.10.0/24 & 212.54.129.0/24 etc..).After telnetting the router it will execute the below commands and below will be the expected output of the commands.From the below outputs it has to search string "best" and below its use all the values againt string "Community:" and AS path values marked bold black & associated to "best" keywords stanza.So the output would look like as below in a text format.
4.21.103.0/24 | 4525:197 4525:1790 4525:1111 | 7323 3549 46133 10.10.10.0/24 | % Network not in table 212.54.129.0/24 | 4525:197 4525:1790 | Local
router>sh ip bgp 4.21.103.0/24 BGP routing table entry for 4.21.103.0/24, version 336364302 Paths: (7 available, best #4) 7423 3549 703 192.168.248.5 (metric 29) from 192.168.103.28 (192.168.103.28) Origin IGP, metric 0, localpref 50, valid, internal Community: 4525:197 4525:1790 4525:9111 Originator: 209.64.228.126, Cluster list: 102.108.201.228 7323 3549 46133 192.168.238.5 (metric 29) from 192.168.103.27 (192.168.103.28) Origin IGP, metric 0, localpref 50, valid, internal,best Community: 4525:197 4525:1790 4525:1111 Originator: 209.64.228.126, Cluster list: 102.108.201.228 router>sh ip bgp 10.10.10.0/24 % Network not in table router>sh ip bgp 212.54.129.0/24 BGP routing table entry for 212.54.129.0/24, version 332786588 Paths: (3 available, best #3) Local, (Received from a RR-client) 212.54.45.235 from 212.54.45.235 (212.54.33.154) Origin IGP, metric 2, localpref 100, valid, internal Community: 4525:197 4525:1790 4525:9111 Local 212.54.33.153 from 0.0.0.0 (212.54.33.153) Origin IGP, metric 11, localpref 100, valid, local, best Community: 4525:197 4525:1790
Below is the script which need to be modified to meet the above results after executing the command "sh ip bgp $prefix"
#!/bin/perl use Net::Telnet::Cisco; $router = "10.10.10.4"; $port = 23; my $cs = Net::Telnet::Cisco->new( Host => $router, Input_log => "Input22.log", Port => $port,); $cs->login( 'cisco', 'cisco' ); $password="cisco123"; $cs->enable( $password ); open(OUTPUT, "> $ARGV[1]") or die "ERROR opening $ARGV[1]: $!n"; open(PREFIXES, "< $ARGV[0]") or die "ERROR opening $ARGV[0]: $!n"; open(ASFILE, "< asnames.txt") or warn "Can not open asnames.txt: $!n"; while (<ASFILE>) { /AS(d+?) (.*)/; $as{$1} = "$2"; } close(ASFILE); $path_num = 0; @cmd_output = $cs->cmd( 'term len 0' ); while (<PREFIXES>) { chomp; if (!/^#/) { $prefix = $_; @cmd_output = $cs->cmd( "sh ip bgp $prefix" ); foreach (@cmd_output) { chomp;
Need commands after chomp;. Anticipating a quick and favourable response. Best Rgds Amit.

Replies are listed 'Best First'.
Re: Need help on perl for Cisco router command execution.
by CountZero (Bishop) on Aug 15, 2009 at 10:57 UTC
    Anticipating a quick and favourable response
    What have you tried yourself?

    Question: why do you accept as an answer 7323 3549 46133 and not 7423 3549 703?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James