每当正文包含 ssh 时,Bash while 循环仅迭代一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7411052/
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:46:40  来源:igfitidea点击:

Bash while loop iterates only once whenever body contains ssh

bashssh

提问by roland

I'm reading host information from a text file and pass it to an ssh command: The text file contains the host, user and password for the ssh command

我正在从文本文件中读取主机信息并将其传递给 ssh 命令:文本文件包含 ssh 命令的主机、用户和密码

while read LINE
do
    R_USER=$(echo $LINE | cut -d ',' -f 1)                  
    R_HOST=$(echo $LINE | cut -d ',' -f 2)                                  
    PY_SCRIPT=$(echo $LINE | cut -d ',' -f 4)               

    ssh $R_USER@$R_HOST 'touch /home/user/file_name.txt'

done </path_name/file_name

As it turns out the while loop is only executed once even if the host text file contains multiple host information. When I remove the ssh command the while loop gets executed as much as there are lines in the host information text file.

事实证明,即使主机文本文件包含多个主机信息,while 循环也只执行一次。当我删除 ssh 命令时,while 循环的执行次数与主机信息文本文件中的行数一样多。

Not sure why this is so. Any information on this?

不知道为什么会这样。有这方面的信息吗?

Roland

罗兰

回答by Jugal Shah

The default standard input handling of ssh drains the remaining line from the while loop.

ssh 的默认标准输入处理会从 while 循环中排出剩余的行。

To avoid this problem, alter where the problematic command reads standard input from. If no standard input need be passed to the command, read standard input from the special /dev/nulldevice:

为避免此问题,请更改有问题的命令读取标准输入的位置。如果不需要将标准输入传递给命令,则从特殊/dev/null设备读取标准输入:

while read LINE
  do
    R_USER=$(echo $LINE | cut -d ',' -f 1)                  
    R_HOST=$(echo $LINE | cut -d ',' -f 2)                                  
    PY_SCRIPT=$(echo $LINE | cut -d ',' -f 4)               

    ssh $R_USER@$R_HOST 'touch /home/user/file_name.txt' < /dev/null

done </path_name/file_name

Or alternatively, try using ssh -nwhich will prevent ssh from reading from standard input. For instance:

或者,尝试使用ssh -nwhich 将阻止 ssh 从标准输入中读取。例如:

ssh -n $R_USER@$R_HOST 'touch /home/user/file_name.txt'

回答by johnnyB

If the file is white space separated

如果文件以空格分隔

host1 user password
host2 user password

then a simple read loop:

然后是一个简单的读取循环:

while read -r Server User Password
do
   /usr/bin/ssh -n $User@$Server touch /home/user/file_name.txt
done </path/to/file.list

But you will be prompted for the password. You cannot pass the "Password" to ssh so I'd suggest storing passwordless ssh-keys and placing them on each host for the user you are connecting as. If youare running this command from a script you can ssh as these users on each host by placing your public key in the user's~/.ssh/authorized_keys(or authorized_keys2) file. If you have the ssh-copy-idcommand you could do this by:

但是系统会提示您输入密码。您不能将“密码”传递给 ssh,因此我建议您存储无密码的 ssh 密钥并将它们放置在您连接的用户的每台主机上。如果从脚本运行此命令,您可以通过将您的公钥放在用户的~/.ssh/authorized_keys(或 authorized_keys2)文件中,以这些用户的身份在每个主机上使用 ssh 。如果您有ssh-copy-id命令,则可以通过以下方式执行此操作:

ssh-copy-id user@hostname 

which would copy YOURssh-key to theirauthorized_keys file so you could then ssh as them. This is assuming you have permission but then again you have their password so what permission do you need?

这会将您的ssh-key复制到他们的authorized_keys 文件,以便您可以像他们一样 ssh。这是假设您有权限,但您又拥有他们的密码,那么您需要什么权限?