#!/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; # Insert a single ASN of a well-connected peer in place # of PEER. Alternately you can cycle through all peers, # but this produces redundant output. If you do that, # run the output file through sort -n | uniq before # sharing it. :) if ($_ =~ /^ PEER /) { ($asn_path) = split /:/; $asn_path =~ s/^ //; $asn_path =~ s/, (aggre..*//; @asn_len = split(/ /, $asn_path); $source_asn = $asn_len[$#asn_len]; printf(OUTPUT "%-5.5s %-15s (%s)n", $source_asn, $prefix, $as{$s +ource_asn}); $path_num++; } } } } $cs->close; exit;
The above Script I downloaded from a site which will telnet into my cisco router with the defined user name & password and execute the commands "term len 0" & then "sh ip bgp $prefix".The output of the commands could result into three types of outputs as shown below.Here from the outputs of commands I want to search & choose the stanza with "best" keywords from the output and copy all values written against keyword "Community:" below "best" else the result is % Network not in table will be entered in another text file againt the value $prefix and also dump "Local" or "7323 3549 46133" values in the same text file.
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 46133 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

In reply to Perl Script help 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.