使用 shell 脚本在远程计算机上运行脚本后终止连接的问题
这是我第一次写shell脚本。我尝试做尽可能多的研究,以避免愚蠢/重复的问题。如果是重复/愚蠢的问题,请原谅。
我有一个 shell 脚本,它连接到远程 Linux 机器并在那里运行脚本。我正在使用“expect”生成 ssh 连接并发出命令来触发作业。但是,我在完成工作后关闭连接时遇到问题。
这是我的脚本:
set prompt "(%|#|\\$|%\]) $"
expect -c 'spawn ssh $UN@$STAGE ;
expect password ; send "$PASS \n";
expect -regexp "$PROMPT"; send "./settings.$UN.sh > settings_log.txt \n";
interact'
该脚本成功地为我运行了脚本文件($UN 和 $STAGE 参数被输入到脚本中。为了简单起见,我在此处省略了它)。然而,这给我留下了一个开放的联系。 我尝试在运行脚本后使用以下而不是上面的方法关闭连接
expect -c 'spawn ssh $UN@$STAGE ;
expect password ; send "$PASS \n";
expect -regexp "$PROMPT"; send "./settings.$UN.sh > settings_log.txt \n";
expect -regexp "$PROMPT"; send "exit \n"'
这确实关闭了连接,但我注意到我的脚本文件根本没有运行。此外,根本不会生成settings_log.txt。
这是否意味着该 exit 命令在进程完成之前中止该进程?我尝试在退出前使用“睡眠”,但没有帮助。使用expect时是否有更好的建议方法来终止连接? 任何帮助表示赞赏。
This is the first time I am writing a shell script. I tried to do as much research as I can to avoid dumb/repetitive question. Please excuse if its repeat/dumb question.
I have a shell script which connects to remote linux machine and runs scripts there. I am using 'expect' to spawn a ssh connection and to issue commands to trigger the job. However, I am having issues while closing the connection after completing the job.
This is my script:
set prompt "(%|#|\\$|%\]) $"
expect -c 'spawn ssh $UN@$STAGE ;
expect password ; send "$PASS \n";
expect -regexp "$PROMPT"; send "./settings.$UN.sh > settings_log.txt \n";
interact'
This script successfully runs the script file for me ($UN and $STAGE parameters are input to the script. I omitted that here for simplicity). However, this leaves me with an open connection.
I tried to close the connection after running the script by using following instead of above
expect -c 'spawn ssh $UN@$STAGE ;
expect password ; send "$PASS \n";
expect -regexp "$PROMPT"; send "./settings.$UN.sh > settings_log.txt \n";
expect -regexp "$PROMPT"; send "exit \n"'
This does close the connection but I noticed that my script file did not run at all. Also the settings_log.txt is not generated at all.
Does this mean, that exit command is aborting the process before its completion? I tried using 'sleep' before exit but it did not help. Is there a better suggested way to terminate the connection when using expect?
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用expect,您可以使用
\r
而不是\n
终止发送命令,因此with expect, you terminate your send commands with
\r
not\n
, so请注意,您可以直接使用 ssh 和 scp 执行远程 shell 命令并复制文件,而无需使用 expect。
例如,
一旦 whatever-you-need-to-execute 完成,连接就会关闭。
Note you can execute remote shell commands and copy files using ssh and scp, directly, without using expect.
For example,
The connection will close as soon as soon as whatever-you-need-to-execute completes.
您的外部脚本似乎是用 csh 编写的,并设置了一个名为“prompt”的变量,但您的
expect
脚本使用了一个名为“PROMPT”的变量。尝试使两个变量名称匹配大小写。Your outer script seems to be written in csh and sets a variable named "prompt", but your
expect
script is using a variable called "PROMPT". Try making the two variable names match case.