简体   繁体   English

如何在后台运行eval spawn ssh和期望?

[英]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. 我创建了一个脚本来使用ssh进入访问点(AP),并执行一些命令,并且我expect与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. 当脚本在前台运行时,这些脚本可以正常工作,但是当我尝试使用&派生该进程在后台执行相同的脚本时,它会在以下ap_commands脚本的spawn ssh命令处停止。

Trying to execute: 尝试执行:

$ run_ap &

Bash script ( run_ap ) to call the expect script: Bash脚本( run_ap )调用期望脚本:

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

Tcl and expect script ( ap_commands ): Tcl和期望脚本( 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 "pass12345\r"

Update 1 - Solution 更新1-解决方案

Thanks to Donal Fellows, I solved using the screen command who it creates a new terminal and execute the script in foreground. 感谢Donal Fellows,我使用screen命令解决了它创建新终端并在前台执行脚本的问题。

screen -dm bash ./run_ap

and I add -f arguments to expect , to force to run in foreground: 我将-f参数添加到expect ,以强制在前台运行:

 #!/usr/bin/expect -f

The interact command will not work with a backgrounded script, since it explicitly expects to interact with the user (as well as the spawn ed subprocess); interact命令不能与后台脚本一起使用,因为它明确希望与用户进行交互 (以及spawn子进程)。 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. 当发生interact时,如果进程不在前台,则操作系统会向其发出信号(SIGTSTP),该信号会停止并等待用户将其设为前台进程。 This is usual Unix terminal handling. 这是通常的Unix终端处理。

If you want to use backgrounding, you need to stick to expect ing things. 如果您想使用背景,则需要坚持expect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM