I am writing a script to run ssh so as to login a remote host, after all the operation is done, I type exit and log off. But I want the script to continue running and write log on the local host. The script is something like:
#!/usr/bin/expect
spwan ssh qwerty@remote_host
expect {
"password:" {
send "123123\r"
}
}
interact;
send "echo $(date) >> login_history.log\r"
But the last command "send …" always failed with the error message like
"send: spawn id exp4 not open …"
When I log off from the remote host, can the expect script continue to work as it is running on the local host?
YES, processing can continue after an [interact].
Short answer: change the last {send …} to {exec date >> login_history.log}
There are several concepts you’ll want to understand to achieve the control flow you’re after. First, http://www.cotse.com/dlf/man/expect/interact_cmd_desc.htm provides a succinct synopsis and example of intermediate [interact] use.
Second: why did you see the message “… spawn id … not open …”? Because the spawn id is not open. The script you wrote said, in effect, “interact, then, after interact is over, send a new command to the ssh process.” If you’ve already logged out, then, of course that id for a defunct process is no longer available.
Third: how do you achieve what you want? I’m unsure what you want. It sounds as though it would be enough for you simply to transform the [send] as I’ve described above. How does that look to you?