期待命令 - Unix

发布于 2024-09-12 23:56:17 字数 510 浏览 10 评论 0原文

我正在使用“expect”来自动执行 ssh 密码身份验证。当我在 SunOS 中运行脚本时,我发现一旦下面的脚本完成,生成的 ssh 进程就会被终止。 Linux 中并非如此。我们该如何避免呢?我们应该以某种方式忽略 SIGCHLD 信号吗? 无论如何,是否可以通过此脚本确定生成的进程是否成功并报告错误(如果有)?

#!/usr/local/bin/expect -f

set password blah-blah
spawn ssh -NfL 8002:<test domain>:22 [email protected]
expect "* password:*"
send -- "$password\r"
send -- "\r"
expect EOF

-卡蒂克

I am using 'expect' to automate ssh password authentication. When I run the script in SunOS, I find the spawned ssh process gets killed once the below script is completed. This is not the case in Linux. How do we avoid it? Should we ignore the SIGCHLD signal somehow?
Is there anyway to determine through this script if spawned process is successful and report error if any?

#!/usr/local/bin/expect -f

set password blah-blah
spawn ssh -NfL 8002:<test domain>:22 [email protected]
expect "* password:*"
send -- "$password\r"
send -- "\r"
expect EOF

-Karthik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

打小就很酷 2024-09-19 23:56:17

如果您使用 ssh-keys,则无需在 shell 脚本中编写密码。

您甚至可以使用密码加密密钥,并使用 ssh-agent 为您管理密钥 - 您在早上解锁密钥,启动隧道,然后在出发时忘记了密钥吃午饭,下午解锁钥匙,晚上回家又忘记了。没有通往远程计算机的磁盘魔法网关。

If you use ssh-keys, you won't need to code passwords in shell scripts.

You could even encrypt the key with a passphrase, and use ssh-agent to manage the key for you -- you unlock your key in the morning, start your tunnel, and then forget your key when you head to lunch, unlock your key in the afternoon, and forget it again when you go home at night. No on-disk magic gateway to remote machines.

盛夏已如深秋| 2024-09-19 23:56:17

您可以将 Expect 脚本放入后台,而不是将 ssh 命令放入后台:

#!/usr/local/bin/expect -f

if {[fork] != 0} exit
disconnect

set password blah-blah
spawn ssh -NL 8002:localhost:22 [email protected]
expect {
    EOF {exit 1}
    "assword:" {}
}
send -- "$password\n"
send -- "\n"
expect EOF
wait

在 Linux 上适用于我。至少在准备阶段,停止它是比较困难的。我必须kill -9 来停止expect 脚本。这可能还需要终止 ssh 进程。

Instead of putting the ssh command in the background you could put the expect script into the background:

#!/usr/local/bin/expect -f

if {[fork] != 0} exit
disconnect

set password blah-blah
spawn ssh -NL 8002:localhost:22 [email protected]
expect {
    EOF {exit 1}
    "assword:" {}
}
send -- "$password\n"
send -- "\n"
expect EOF
wait

Works for me on Linux. At least for the setup phase, stopping it is more difficult. I had to kill -9 to stop the expect script. Which probably requires killing the ssh process as well.

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