使用 shell 脚本在远程计算机上运行脚本后终止连接的问题

发布于 2024-10-16 00:15:50 字数 981 浏览 5 评论 0原文

这是我第一次写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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_畞蕅 2024-10-23 00:15:50

使用expect,您可以使用 \r 而不是 \n 终止发送命令,因此

 expect -c 'spawn ssh   $UN@$STAGE
            expect password
            send "$PASS\r"
            expect -regexp "$PROMPT"
            send "./settings.$UN.sh > settings_log.txt\r"
            expect -regexp "$PROMPT"
            send "exit\r"
            expect eof'

with expect, you terminate your send commands with \r not \n, so

 expect -c 'spawn ssh   $UN@$STAGE
            expect password
            send "$PASS\r"
            expect -regexp "$PROMPT"
            send "./settings.$UN.sh > settings_log.txt\r"
            expect -regexp "$PROMPT"
            send "exit\r"
            expect eof'
小女人ら 2024-10-23 00:15:50

请注意,您可以直接使用 sshscp 执行远程 shell 命令并复制文件,而无需使用 expect

例如,

scp ./settings.$UN.sh $UN@$STAGE:settings_log.txt
ssh $UN@$STAGE whatever-you-need-to-execute

一旦 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,

scp ./settings.$UN.sh $UN@$STAGE:settings_log.txt
ssh $UN@$STAGE whatever-you-need-to-execute

The connection will close as soon as soon as whatever-you-need-to-execute completes.

笑着哭最痛 2024-10-23 00:15:50

您的外部脚本似乎是用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文