Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expect exec or something like it question

Status
Not open for further replies.

grepper

Technical User
Jun 4, 2003
49
US
Hey guys, I have an Expect question. I'm trying to capture the result of a send back to the console after spawning a telnet to another server. Kinda like

spawn telnet servera
send $command
then display the result of the $command back to the console with out all the other login and prompt stuff.

I can put a log_user before and after the send command i.e.

log_user 1 ;#display to the console
send -- "$command\r"
expect timeout {send_user "Timed out waiting for prompt\n"; exit} -re $prompt
log_user 0 ;#do not display to the console

but it also sends the prompt back to the display. I thought the exec or system statement might do it but it runs the exec on the server I'm running the script from and not the server I've spawned to.

set result [exec $command]
send_tty $result

I'm basically writting an Tcl/Expect script to go out to all my servers (120+) and get info for reporting, i.e. oslevel or check cron entries whatever I set $command to. Everything works great I just would like to clean up the results so others can read it. Thanks in advance.
 
The best way to do this, and the only way really, is
to expect the output from your send.

Say for example you want the full output of ls
after you send it to your spawned session.
Something like this:
Code:
#!/usr/bin/tclsh
package require Expect

log_user 0
#exp_internal 1
set timeout 125
set prompt ".*@.*"


spawn -noecho /bin/sh


      expect {

               -re "$prompt" {
                             send "ls -l\r"
                             expect -re "$prompt" {send_user $expect_out(buffer) ; interact}
               }

               timeout "Timed out waiting for $prompt"
               eof "Spawned process died unexpectedly"
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top