Skip to content
Advertisement

How to run eval spawn ssh and expect in background?

I created a script to get in inside an Access Point (AP) using ssh and execute some commands and I used expect to interact with shell.

The scripts works fine when it is running on foreground, but when I try to execute the same script in background using & to fork the process, it stop at spawn ssh command at ap_commands script below.

Trying to execute:

$ run_ap &

Bash script (run_ap) to call the expect script:

#!/bin/sh
while(true); do
    /script/ap_commands
done

Tcl and expect script (ap_commands):

#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user@192.168.92.1

#use correct prompt
set prompt ":|#|\>"
set user   ">"

interact -o -nobuffer -re "Password:" return
send "pass12345r"

Update 1 – Solution

Thanks to Donal Fellows, I solved using the screen command who it creates a new terminal and execute the script in foreground.

screen -dm bash ./run_ap

and I add -f arguments to expect, to force to run in foreground:

 #!/usr/bin/expect -f

Advertisement

Answer

The interact command will not work with a backgrounded script, since it explicitly expects to interact with the user (as well as the spawned subprocess); when the interact happens, if the process isn’t in the foreground, the OS gives it a signal (SIGTSTP) that makes stop and wait for the user to make it a foreground process. This is usual Unix terminal handling.

If you want to use backgrounding, you need to stick to expecting things.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement