I am trying to collect logs from a system in expert mode. Where I login to system and then change mode to expert mode and issue command to collect the log. I have a script which works but It waits for 30 mins even though log collection is complete before that. I have two issues with existing script.
-
Since log collection takes really long time (sometimes about 30 minutes). Expect script does not wait for the prompt and exits at timeout value. So I don't get complete O/P. Hence, I added timeout value of 1800 secs. But this one forces script to wait for 30 mins. I would like to exit as soon as I am back on prompt. I tried using expect exact but need some help on this.
-
How can I capture the current prompt itself as a variable and ask Expect to wait until it gets the prompt. or Make a prompt based on Username variable and hostname lie - USERNAME@SYSNAME##.
Here is my current code.
#!/usr/bin/expect -f
set timeout 5
set hostname [lindex $argv 0]
set username "admin"
set password "password"
set prompt "(>|#|\\\$) $"
spawn ssh $username@$hostname
expect {
".*key fingerprint" {send "yes\r"; exp_continue}
"assword:" {send "$password\r"}
}
expect -re {\$prompt}
send "show product id\r"
expect -re "\nProduct ID: (.*)$prompt"
set PRDID $expect_out(1,string)
#send "hostname\r"
#expect -re "\n (.*)$prompt"
#set HNAME $expect_out(1,string)
expect -re $prompt
send "expert mode\r"
expect -re "assword:" {send "$PRDID\r"}
#send "$PRDID\r"
expect -re $prompt
#set output $expect_out(buffer);
send "show detailed logs\r"
set timeout 1800 ### To wait for 30 mins
## expect -re $prompt
expect -exact "(.*)## $" ### I tried using expect exact. But it seems something wrong here.
### expect -exact "\nSA@$HNAME##$"
send -- "\r"
send "exit\r"
#puts [open w] $expect_out(buffer);
expect eof
[root@se scripts]#