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
Expect: capture unix cmd into var over ssh

Posted by self1sm (self1sm), 21 January 2005
I am just plain stuck.

I am using expect to ssh into remote host and cat various files. I am trying to capture the output from the cat command to a variable in my script so I can write it to a file. I don't seem to be able to match correctly:
set prompt "(%|#|>|\\\$) $"

expect {
  -nocase -re "linux*" { send -- "cat /etc/passwd\r"

exp_internal 1

expect -re $prompt {
 set password_file [open "$host-password.txt" w]
 puts $password_file $expect_out(buffer)

The exp_internal shows the match on "linux*" but nothing for the prompt after sending the "cat /etc/passwd"

Please help.

Posted by selfism (selfism), 22 January 2005
I have rewritten that code a hundred ways... let me get back to the basics:

Presumming an Expect script running on systemAA has sshed me to systemBB, how do I send systemBB a command and store the (result) of that command into a file/variable on systemAA?

This is how I understand how to do it:

send "cat /etc/passwd\r"
expect -re $prompt
set my_file [open "$host-my-file.txt" w]
puts $my_file $expect_out(buffer)

I am on my second day with this part of my code and am totally stuck. Can someone lend me another pair of eyes?

Posted by neo (The Architect), 22 January 2005
Just guessing maybe your 'expect' is timing out before 'cat' completes?

Posted by admin (Graham Ellis), 22 January 2005
What do you see on the screen to trace through what you're doing?   Does your script have a
    log_user 0
somewhere in it earlier on?

If so, take out that log user and you should see a verbose display when you run that gives you a good insite as to where it's jamming.

Posted by selfism (selfism), 23 January 2005
First of all thank you for helping out !!

It was actually the code above the peice I posted that was having the  issue. Before sending the "cat /etc/passwd" it would timeout on the adminpassword and I wasn't picking that up. Anywho here is the way the code block should have looked:

proc linux_cmds {} {
global prompt server_dir host
send -- "cat /etc/passwd\r"
expect -re "$prompt"
set password_file [open "$server_dir/$host/$host-password.txt" "w"]
puts $password_file $expect_out(buffer)
close $password_file

send -- "su - root\r"
expect -exact "assword: "

sleep 2  <------------------------------expect was sending before  the remote OS terminal driver  was ready. As soon as I put sleep in it worked!!

send -- "$adminpassword\r"
expect -re "$prompt"

expect {
  -nocase -re "linux*" { linux_cmds }

This is a great forum, I will help out if at all possible. Thanks again!

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