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,

Here is an example:

$script outputTemp.txt
Script started, output file is outputTemp.txt
Connected to
Escape character is '^]'.
-------------- ---------------
---------  AT&T IP Services Route Monitor  -----------
ogin: rviews

--- JUNOS 12.1R3-S4 built 2013-03-24 11:22:31 UTC>> show route
inet.0: 446525 destinations, 6697636 routes (446525 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both          *[Static/5] 6d 20:48:59
                    > to via em0.0         *[BGP/170] 3d 07:35:35, localpref 100, from
                      AS path: 7018 15169 I
                    > to via em0.0> exit

Connection closed by foreign host.

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. :) 


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


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

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