Friday, June 7, 2013

Capture SSH Session output with Script

As network engineers, we often need to capture outputs of routers/switches to analyze, manipulate, or stored. When we need to do it systematically on large number of routers, we can use Expect or PExpect as covered in other blog entries, Tcl Expect with Python and PExpect Module.

But what if all we need is to capture the output with just one router? Maybe it has a acl that spans pages and pages, or you are on a box that your original script cannot reach. Whatever the reason it may be, you can use the *nix 'script' command to make a type script of the output, http://linux.about.com/library/cmd/blcmdl1_script.htm.

Here is an example:

$script outputTemp.txt
Script started, output file is outputTemp.txt
$telnet route-server.ip.att.net
Trying 12.0.1.28...
Connected to route-server.cbbtier3.att.net.
Escape character is '^]'.
-------------- route-server.ip.att.net ---------------
---------  AT&T IP Services Route Monitor  -----------
<skip>
ogin: rviews
Password:

--- JUNOS 12.1R3-S4 built 2013-03-24 11:22:31 UTC
rviews@route-server.ip.att.net>
rviews@route-server.ip.att.net> show route
<skip>
inet.0: 446525 destinations, 6697636 routes (446525 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both

0.0.0.0/0          *[Static/5] 6d 20:48:59
                    > to 12.0.1.1 via em0.0
1.0.0.0/24         *[BGP/170] 3d 07:35:35, localpref 100, from 12.122.83.238
                      AS path: 7018 15169 I
                    > to 12.0.1.1 via em0.0
views@route-server.ip.att.net> exit

Connection closed by foreign host.
$exit
exit

Script done, output file is outputTemp.txt
$ls -lia outputTemp.txt
999192 -rw-r--r--  1 ericchou  wheel  4233 Jun  7 07:36 outputTemp.txt
$

90% of the time I use Expect/PExpect for capture router outputs, but for another 10% of the time I am thankful of the 'script' command. 

Happy automation. :) 


4 comments:

  1. Replies
    1. Hi Dhana, it captures whatever is on your screen so output via ssh would work too. :)

      Delete

  2. Thank you so much for sharing this useful information! You saved my time)
    Richard Brown online data room

    ReplyDelete
    Replies
    1. And time is what all of us can have more of.. :) Thanks for the comment, Richard.

      Delete