Capture output from ssh with expect script

Omnomnom picture Omnomnom · May 7, 2015 · Viewed 16.9k times · Source

I want to capture the output of my expect script in a file.txt.

#!/usr/bin/expect -f

match_max 10000
set timeout 120
eval spawn ssh 10.0.0.0
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "password\r"
expect ">"
send "sh cdp neighbors detail\r\r                              "
expect ">"
send "\n"
sleep 5

This is what I have, he reads out my cdp neighbors detail from a switch. But now I want to push this output in a file, in my directory. So I can automate doing commands on a switch, and get the output. The script works completely but I can't find enough information to read the output from an expect script.

Thanks in advance!

Omnomnom

Answer

Dinesh picture Dinesh · May 7, 2015

You can make use of log_file.

log_file switch.log;# Logging it into the file 'switch.log'

Just add that before the line from where you want to capture the logging. Have a look at here for more information.