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.

In reply to Need help on perl for Cisco router command execution. by amit_singh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.