rekrowteN | Networker

All you want to know about computer networks and more

Show Routes from BGP Neighbors by Unix Expect Script

Previous post presented Unix expect and easy script to use. I was sending basic commands like “show ip route”. But if you want to have output of all advertised BGP routes from all BGP neighbors? Is it even possible to automate it? Well, we can create another set of scripts and include them to already known expect.

First, we need to run “show ip bgp summary” and extract all neighbors. Following script runs per device in “shipbgsu” list of devices. It echos only those lines, which are starting with IPv4 address. Only this IPv4 address is appended to a file “nei_shipbgsu”.

When we do have IP addresses, we can simply run command “show ip bgp neighbor XXX advertised-routes”, where XXX is neighbor read from previous file.

And here is the procedure, which will be run in three separate steps. First, already known script from previous post, slightly modified.

1) Create three files. First one is shipbgsu with following BASH script:

#!/bin/bash
while read line
do
  sh expect_shipbgsu "$line" > "$line"-ip-bgp-sum.txt &
done < devices

This just executes expect script from second step. It will create instance in background. There will be as much instances as much lines in devices file we have. Output from telnets are saved to unique files.

2) Create file expect_shipbgsu containing expect commands. You spawn telnet session and do “show ip bgp summary”. It contains all BGP neighbors:

#!/bin/bash
/usr/bin/expect<<EOD
set timeout 3
spawn telnet $1
expect "*>"
send "term len 0\r"
expect "*>"
send "sh ip bgp summ\r"
set timeout 3
expect "R*>"
send "exit\r"
exit
EOD

3) Create list of devices. I am using Dynamips and two routers:

127.0.0.1 2000
127.0.0.1 2001

4) Execute shipbgsu script. This is what you get:

File 127.0.0.1 2000-ip-bgp-sum.txt:

spawn telnet 127.0.0.1 2000 
Trying 127.0.0.1... 
Connected to 127.0.0.1. 
Escape character is '^]'. 
Connected to Dynamips VM "R1" (ID 0, type c2600) - Console port 

{ term len 0 
R1#sh ip bgp summ 
BGP router identifier 10.0.0.1, local AS number 1 
BGP table version is 2, main routing table version 2 
1 network entries using 101 bytes of memory 
1 path entries using 48 bytes of memory 
1 BGP path attribute entries using 60 bytes of memory 
0 BGP route-map cache entries using 0 bytes of memory 
0 BGP filter-list cache entries using 0 bytes of memory 
BGP using 209 total bytes of memory 
BGP activity 1/0 prefixes, 1/0 paths, scan interval 60 secs 

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd 
10.0.0.2        4     1      14      18        2    0    0 00:10:57        0 
172.16.0.2      4     1      14      18        2    0    0 00:10:52        0 
R1#

File 127.0.0.1 2001-ip-bgp-sum.txt:

spawn telnet 127.0.0.1 2001 
Trying 127.0.0.1... 
Connected to 127.0.0.1. 
Escape character is '^]'. 
Connected to Dynamips VM "R2" (ID 1, type c2600) - Console port 

{ term len 0 
R2#sh ip bgp summ 
BGP router identifier 172.16.0.2, local AS number 1 
BGP table version is 2, main routing table version 2 
1 network entries using 101 bytes of memory 
2 path entries using 96 bytes of memory 
1 BGP path attribute entries using 60 bytes of memory 
0 BGP route-map cache entries using 0 bytes of memory 
0 BGP filter-list cache entries using 0 bytes of memory 
BGP using 257 total bytes of memory 
BGP activity 1/0 prefixes, 2/0 paths, scan interval 60 secs 

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd 
10.0.0.1        4     1      18      14        2    0    0 00:10:56        1 
172.16.0.1      4     1      18      14        2    0    0 00:10:52        1 
R2#

5) Extract neighbors from these outputs. Create yet another script and execute it. Script is called shipbgsu_extract:

#!/bin/bash
while read line
do
  rm -f "$line"-ip-bgp-nei.txt
  while read -r line2; do
    echo $line2 | grep -o "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" >> "$line"-ip-bgp-nei.txt
  done < "$line"-ip-bgp-sum.txt
done < devices

You will get two new files, because you have two lines in devices:

File 127.0.0.1 2000-ip-bgp-nei.txt:

10.0.0.2
172.16.0.2

File 127.0.0.1 2001-ip-bgp-nei.txt:

10.0.0.1
172.16.0.1

6) Create new file shipbgad, which is used for executing new expects:

#!/bin/bash
while read line2
do
  expect -f expect_shipbgad ${line2[0]} ${line2[1]} > "$line2"-ip-bgp-adroutes.txt &
done < devices

I pass IP address and port as separate arguments or else telnet is not spawning properly. I am passing two arguments only because I have IP and port in “devices” file. If you have IP or hostname only, replace “${line2[0]} ${line2[1]}” by “$line2″.

7) Create new expect script, which will spawn telnets based on arguments from shipbgad script and run “sh ip bgp neighbors” command for all neighbors from appropriate list of neighbors. If you have IP or hostname only, erase “[lindex $argv 1]” from “spawn telnet” and from “set f”, which is used for opening file.

spawn telnet [lindex $argv 0] [lindex $argv 1]
expect "*>"
send "term len 0\r"
expect "*>"
set f [open "[lindex $argv 0] [lindex $argv 1]-ip-bgp-nei.txt"]
while {[gets $f line] >= 0} {
  send "sh ip bgp nei $line advertised-routes\r"
  set timeout 3
  expect "R*>"
}
send "exit\r"
exit

8) Enjoy results. We have two new files:

127.0.0.1 2000-ip-bgp-adroutes.txt has two neighbors, each advertising one route:

spawn telnet 127.0.0.1 2000 
Trying 127.0.0.1... 
Connected to 127.0.0.1. 
Escape character is '^]'. 
Connected to Dynamips VM "R1" (ID 0, type c2600) - Console port 

{ term len 0 
R1>sh ip bgp nei 10.0.0.2 advertised-routes 
BGP table version is 3, local router ID is 172.16.0.1 
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, 
              r RIB-failure, S Stale 
Origin codes: i - IGP, e - EGP, ? - incomplete 

   Network          Next Hop            Metric LocPrf Weight Path 
*> 10.1.1.1/32      0.0.0.0                  0         32768 i 
R1>sh ip bgp nei 172.16.0.2 advertised-routes 
BGP table version is 3, local router ID is 172.16.0.1 
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal, 
              r RIB-failure, S Stale 
Origin codes: i - IGP, e - EGP, ? - incomplete 

   Network          Next Hop            Metric LocPrf Weight Path 
*> 192.168.1.1/32   0.0.0.0                  0         32768 i 
R1>

127.0.0.1 2001-ip-bgp-adroutes.txt has two neighbors and has nothing to advertise:

spawn telnet 127.0.0.1 2001 
Trying 127.0.0.1... 
Connected to 127.0.0.1. 
Escape character is '^]'. 
Connected to Dynamips VM "R2" (ID 1, type c2600) - Console port 

{ term len 0 
R2>sh ip bgp nei 10.0.0.1 advertised-routes 

R2>sh ip bgp nei 172.16.0.1 advertised-routes 

R2>

Of course, it can be done in fewer steps and more automated way.

About these ads

3 responses to “Show Routes from BGP Neighbors by Unix Expect Script

  1. alex December 20, 2012 12:51 at 12:51

    shipbgsum_extract minor correction to make (> instead of >>):

    echo $line2 | grep -o “^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}” >> “$line”-ip-bgp-nei.txt
    change it to this line
    echo $line2 | grep -o “^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}” > “$line”-ip-bgp-nei.txt

    thanks you for your help and sharing codes

    • Filip Burda December 20, 2012 15:40 at 15:40

      Hello Alex. For this script, double greater than sign is needed (“>>”). In case of having only one greater than sign, output file will be overridden in every cycle. Purpose here is to catch all BGP neighbors into one file, one at a line. Then, another script executes appropriate command by cycling through this file, containing BGP neighbors.

      Again, script is not efficient, but it is somehow straightforward and easier to follow. Better to have not optimal code and spend little time programming than creating super algorithms and spending lot of time programming it :)

      • alex December 20, 2012 17:48 at 17:48

        you are right it was just a comment on the fly ;)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: