期待命令 - Unix
我正在使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 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.您可以将 Expect 脚本放入后台,而不是将 ssh 命令放入后台:
在 Linux 上适用于我。至少在准备阶段,停止它是比较困难的。我必须
kill -9
来停止expect 脚本。这可能还需要终止 ssh 进程。Instead of putting the ssh command in the background you could put the expect script into the background:
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.