Using Expect to get cisco running configs
Posted by soop (soop), 12 March 2008I'm hoping someone can help me - I've yet to find a "simple" guide to expect anywhere (and one would be appreciated). I've searched google, and I'm only getting very vague clues as to what I'm doing.
[scenario] Expect is being run from box1, telnetting to box2, and from there accessing the different routers (it needs to be this way because of the security set-up) to get their running config and exporting it to box1.
[problem] I was expecting to use something like:
send "show run conf\r"
set results $expect_out(buffer)
(or even better output to a file), on the Cisco. But of course, all I got was my login to box2.
So somehow, I need to export the buffer from box2's instance of expect to box1 (I think?!). I've seen examples of things that sound like they do this, but I have no idea what all the */r etc does.
If anyone could give me a straightforward answer I would be indebted.
Posted by admin (Graham Ellis), 13 March 2008The best way for me to describe your expect session is that you should choresograph it - make a note of all of the actions necessary to do the job manually and then write them into a send and expect sequence. The \r is what you put in when you're sending a new line.
BUT ... expect programming isn't easy; you have your own program and one or more remote processes running at the same time, so it's not as straightforward as normal coding. As regards a "simple guide" then, not really - it's one of those subjects best learnt with tutorial assistance as you need to see how all the interactions work and the effect of various timing and matching issues in real life.
Posted by soop (soop), 13 March 2008I fixed it - I was spawning a telnet session on box2 rather than just sending "telnet [ip address]".
I still currently need help exporting the output from the router into a file on box1, so if you could give me any pointers, that'd be great.
Don't worry, done it after browsing around the archives.
For future reference, (in case anyone else has the same problem) set a variable at the top, like this:
set outputvariable [open "file.txt" "w+" 0666 ] ;
which means "set this variable to open file.txt in write mode with global read/write permissions"
And then underneath
put puts $outputvariable "[string trimright $expect_out(buffer) "\n"]";
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho