Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/Expect-capture-unix-cmd-into-var-over-ssh.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))
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:
[snip]
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)
[/snip]  

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 !!

SOLUTION:
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 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