Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/Using-Expect-to-get-cisco-running-configs.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Using Expect to get cisco running configs

Posted by soop (soop), 12 March 2008
I'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"
expect "prompt"
set results $expect_out(buffer)
puts $results
(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 2008
The 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 2008
I 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.

*edit*
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

send [command]
expect [prompt]

put  puts $outputvariable "[string trimright $expect_out(buffer) "\n"]";






This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho