Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

Phone System reconfiguration 11.1.2018 to 26.1.2018 - we are on email but incoming landline not available. Temp number 0797 4 925928
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.

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 and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

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