How to have Expect script output to file
Asked Answered
E

0

2

I need to gather some statistics from a network switch and would like to use an expect script to output the data to a file. I would like to run this script as a cron job and have the data appended to the file when it is run. Following is the working code I have so far, I just do not know how to get the output to a file. Any help is greatly appreciated.

 #!/bin/bash
 #get mac-address count
 /usr/bin/expect -f -<<EOD

 spawn ssh [email protected]
 sleep 2

 #Catch the password prompt and send supplied password
 expect {
 "*word:" {send "password\r"}
 }
 sleep 1

 #Get into enabled mode
 expect {
 "*>" {send "system-view\r"}
 }
 sleep 1

 expect {
 "*]" {send "display mac-address count\r"}
 }
 sleep 1

 expect { 
 "*]" {send "quit\r"}
 }
 sleep  1

 expect {
 "*>" {send  "quit\r"}
 }
 sleep 1

 expect eof
 EOD
Epicycle answered 29/1, 2016 at 2:42 Comment(4)
What happens if you add exec &>/path/to/logfile to the beginning of the script?Lei
Add log_file output.log at the start of the script.Webfoot
Dinesh, your option seems to work best as it appends count everytime it is run. Thank you.Epicycle
This helped me solve OSX Xcode license acceptance problem. #26125536Priestly

© 2022 - 2024 — McMap. All rights reserved.