通过SSH运行shell时如何自动输入? [英] How to input automatically when running a shell over SSH?
问题描述
在我的 shell 脚本中,我正在运行一个要求我输入的命令.
In my shell script I am running a command which is asking me for input.
如何自动为命令提供所需的输入?
How can I give the command the input it needs automatically?
例如:
$cat test.sh
ssh-copy-id tester@10.1.2.3
运行 test.sh 时:
When running test.sh:
首先,它会问:
First, it will ask:
您确定要继续连接吗(是/否)?
然后,它会要求我输入密码:
Then, it will ask me to input the password:
tester@10.1.2.3 的密码:
有没有办法自动输入这个?
Is there a way to input this automatically?
推荐答案
对于一般的命令行自动化,Expect 是经典工具.或者,如果您更熟悉 Python,请尝试 pexpect.
For general command-line automation, Expect is the classic tool. Or try pexpect if you're more comfortable with Python.
这里有一个类似的问题,建议使用 Expect:在bash脚本中使用expect为SSH命令提供密码
Here's a similar question that suggests using Expect: Use expect in bash script to provide password to SSH command
这篇关于通过SSH运行shell时如何自动输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!