rekrowteN | Networker

All you want to know about computer networks and more

Unix Expect Script

I have simple task for you. Do “show ip bgp” and “show ip route” and save output into file. We know that. OK and now do it from 900 devices and be quick. Oops. It is time to warm up Unix console and bash.

To remotely connect to a device, you can use “expect”. It must be installed extra to your Unix machine, if you don’t have this package. Simple config is presented below. If you have AAA configured, you provide username and password. Normally, you go to privileged mode, where another password must be provided. So, you are expecting “Username” on the prompt and you send your username “abcdef”. Then you expect “Password” on the prompt and you provide your password. Then you expect prompt with some hostname, represented by asterisk here and with “>” sign. We don’t need to go to privileged mode, so I can start do the task.

But before that, I set terminal length to 0 (better said – infinity), so it shows the whole output at once. Then I do “sh ip bgp” and set timeout for 300 seconds. So I will wait no longer than 300 seconds before expecting whole output from that command. You see the change in expect? I expect prompt starting with “R”, because all my routers start with letter “R”. If I put asterisk and greater than sign, it might match something from BGP output.

Then I send command “show ip route”, wait no more than 300 seconds and expect prompt with hostname. And exit and we are done. Here is the original file for expect:

#!/bin/bash
/usr/bin/expect<<EOD
set timeout 900
spawn telnet $1
expect "sername:"
send "abcdef\r"
expect "assword:"
send "test1234\r"
expect "*>"
send "term len 0\r"
expect "*>"
send "sh ip bgp\r"
set timeout 300
expect "R*>"
send "show ip route\r"
set timeout 300
expect "R*>"
send "exit\r"
exit
EOD

I am now on Dynamips and I don’t have AAA authentication. I will remove excessive lines and this is what I need:

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

This file is called “expect_script”. I have decreased timeout, as it is run locally, so it is fast. No username and password lines are present. OK, so what can I do is to execute this script 900 times; every time for different device to different output file. Or I can create another small script. Which one it is? Script!

Following script is called “shipbg” as abbreviation from show ip bgp. It runs in while loop. It reads from file “devices” line by line device IP and port (because I am on Dynamips, in reality, you most likely need IP only for telnet or hostname). Then it runs “expect_script” and send as parameter, what was read from “devices” file. Everything is sent to output file, which has the same name as is in “devices”, followed by “-ip-bgp.txt”. It is run in background. Without ampersand at the end, it will be run one by one. When you have 900 devices, it takes some time. With this, you run it in parallel, all 900 at once. And I have all my outputs under 2 minutes! You run shipbg script from your console and that’s it. “expect_script” on line 4 calls script above.

Here is shipbg script:

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

Here is file “devices”:

127.0.0.1 2000
127.0.0.1 2001

Here are output files. First 127.0.0.1 2000-ip-bgp.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 
BGP table version is 2, local router ID is 10.0.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#show ip route 
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP 
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area  
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
       E1 - OSPF external type 1, E2 - OSPF external type 2 
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2 
       ia - IS-IS inter area, * - candidate default, U - per-user static route 
       o - ODR, P - periodic downloaded static route 

Gateway of last resort is not set 

     172.16.0.0/24 is subnetted, 1 subnets 
C       172.16.0.0 is directly connected, FastEthernet0/1 
     10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks 
C       10.0.0.0/24 is directly connected, FastEthernet0/0 
C       10.1.1.1/32 is directly connected, Loopback0 
R1#

And 127.0.0.1 2001-ip-bgp.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 
BGP table version is 2, local router ID is 172.16.0.2 
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 
* i10.1.1.1/32      172.16.0.1               0    100      0 i 
*>i                 10.0.0.1                 0    100      0 i 
R2#show ip route 
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP 
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area  
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2 
       E1 - OSPF external type 1, E2 - OSPF external type 2 
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2 
       ia - IS-IS inter area, * - candidate default, U - per-user static route 
       o - ODR, P - periodic downloaded static route 

Gateway of last resort is not set 

     172.16.0.0/24 is subnetted, 1 subnets 
C       172.16.0.0 is directly connected, FastEthernet0/1 
     10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks 
C       10.0.0.0/24 is directly connected, FastEthernet0/0 
B       10.1.1.1/32 [200/0] via 10.0.0.1, 00:03:08 
R2#
About these ads

6 responses to “Unix Expect Script

  1. Pingback: Show Routes from BGP Neighbors by Unix Expect Script « rekrowteN | Networker

  2. Pingback: Network Data from Unix to Excel: Routes with BGP Community (Part 1) | rekrowteN | Networker

  3. Pingback: Network Data from Unix to Excel: Configured BGP Community Lists (Part 2) | rekrowteN | Networker

  4. Pingback: Network Data from Unix to Excel: Status of WAN Interfaces (Part 3) | rekrowteN | Networker

  5. Pingback: Unix Hints: Filter Specific Lines – Chassis Information (Part 2) | rekrowteN | Networker

  6. Pingback: Unix Hints: ACL Addition Verification with Unix Script (Part 5) | rekrowteN | Networker

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: