在bash shell脚本执行一个循环内ssh命令 [英] Executing ssh command in a bash shell script within a loop
问题描述
我想一个一个bash shell脚本,应执行以下范围内执行ssh命令:
1)ssh来举办
2)执行命令
3)命令打印值
4)重复步骤1-3
5)出口的bash shell脚本
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
我已经设置了密码,少到远程主机条目,增加主机密钥远程主机
I have set up password less entry to the remote host, added host key to remote host
我要测试的httpd进程的远程主机上运行的各种状态
在一个文本文件,httpd_process.txt,我有:
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:
/etc/init.d/httpd状态(停止,启动,重启)
/etc/init.d/httpd status (stop, start, restart)
我在脚本如下:
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?
推荐答案
SSH
消耗标准输入。通过它 -n
来prevent这一点。
ssh
is consuming stdin. Pass it -n
to prevent this.
这篇关于在bash shell脚本执行一个循环内ssh命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!