Omnomnom
Omnomnom

Reputation: 111

Capture output from ssh with expect script

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

Upvotes: 3

Views: 20372

Answers (2)

Dinesh
Dinesh

Reputation: 16428

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.

Upvotes: 6

Omnomnom
Omnomnom

Reputation: 111

Found it, very easy:

./expectscript > output.txt 2>&1

Upvotes: 4

Related Questions