Mike
Mike

Reputation: 975

expect script write to file partial output

I am trying to write part of the output of an expect script ssh-ing to a server to a log file.
Now i am having this script:

#!/usr/bin/expect -f
spawn telnet 192.168.20.222
match_max 10000
expect "*?to continue*"
send -- "\r"
send -- "show interfaces 1 \r"
expect -- "*?2626#*"
send -- "show interfaces 2 \r"
expect -- "*?2626#*"
send -- "exit \r"
expect -- "*?2626>*"
send -- "exit \r"
expect "*?y/n*"
send -- "y \r"

How can i change it to output to a file 'log.txt' just the response from the show interfaces 1 and show interfaces 2 ?

Upvotes: 0

Views: 5201

Answers (1)

user1558113
user1558113

Reputation: 176

...
log_file "log.txt" ;#enable loging to file as well
send -- "show interfaces 1 \r"
expect -- "*?2626#*"
send -- "show interfaces 2 \r"
expect -- "*?2626#*"
log_file; #disable logging
...

The 'log.txt' will contain output from both commands + commands themselves + prompts. Is this sufficient?

Getting pure output may be more complicated, useful technique is to use explicit brackets (not sure whether the system you're interacting with would allow something similar). In shell it would look like:

send -- "show interfaces 1\r echo '<XYZ>'\r"
expect "<XYZ>"; #first echoed when the command was typed
expect {
   "<XYZ>" {
        #for the second time echoed when the command finished
        #here the $expect_out(0,string) contains the command output + the bracket
        regexp {(.*)<XYZ>} $expect_out(0,string) match pure_cmd_output
   }
}

Upvotes: 1

Related Questions