在循环内的 bash shell 脚本中执行 ssh 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6351022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:10:35  来源:igfitidea点击:

Executing ssh command in a bash shell script within a loop

bashssh

提问by Roland

I'm trying to execute an ssh command within a a bash shell script that should perform the following: 1) ssh to host 2) execute command 3) print value of command 4) repeat steps 1 -3 5) exit bash shell script

我正在尝试在 bash shell 脚本中执行 ssh 命令,该命令应该执行以下操作:1) ssh 到主机 2) 执行命令 3) 打印命令值 4) 重复步骤 1 -3 5) 退出 bash shell 脚本

I have set up password less entry to the remote host, added host key to remote host

我已经设置了远程主机的无密码进入,向远程主机添加了主机密钥

I want to test the various states of the httpd process running on the remote host Within a text file, httpd_process.txt, I have:

我想测试在远程主机上运行的 httpd 进程的各种状态 在一个文本文件 httpd_process.txt 中,我有:

/etc/init.d/httpd status (stop, start, restart)

/etc/init.d/httpd 状态(停止、启动、重启)

I do the following in the script:

我在脚本中执行以下操作:

while read LINE
do
    echo "Httpd Request: $LINE"
    status=`$LINE`
    echo "Status: $status"
    sleep 5 # sleep so that next 
done < /path_name/httpd_process.txt

exit 0

I assumed that each time through the loop another input string is read from the input text file and the request is made to the remote host. However, what I experience is that after the first request the script terminates. Am I correct to assume that as the first request is sent it creates a child process and once that process completes my script completes and the next turn through the loop is not executed?

我假设每次通过循环从输入文本文件中读取另一个输入字符串,并向远程主机发出请求。但是,我的经验是在第一个请求之后脚本终止。我是否正确假设在发送第一个请求时它会创建一个子进程,并且一旦该进程完成我的脚本完成并且不执行循环的下一轮?

回答by Ignacio Vazquez-Abrams

sshis consuming stdin. Pass it -nto prevent this.

ssh正在消耗标准输入。传递它-n以防止这种情况发生。