Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于主机之间shell中的循环(使用ssh)_Shell_Ssh - Fatal编程技术网

用于主机之间shell中的循环(使用ssh)

用于主机之间shell中的循环(使用ssh),shell,ssh,Shell,Ssh,我有一个for循环来遍历我的其他amazon实例: #!/bin/bash LIST="ec2xxxA.compute.amazonaws.com ec2xxxB-central-1.compute.amazonaws.com" SSHKEY="ssh -tt -i key.pem centos@" for i in $LIST; do ${SSHKEY}$i hostname 现在,我尝试循环浏览我的列表并打印amazon的两个实例的主机名。 它适用于第一个。但在循环之后,它不

我有一个for循环来遍历我的其他amazon实例:

#!/bin/bash
LIST="ec2xxxA.compute.amazonaws.com ec2xxxB-central-1.compute.amazonaws.com"
SSHKEY="ssh -tt -i key.pem centos@"

  for i in $LIST; do
    ${SSHKEY}$i hostname
现在,我尝试循环浏览我的列表并打印amazon的两个实例的主机名。 它适用于第一个。但在循环之后,它不会更进一步:

./script.sh
ip-172-xx-xx-01
Connection to ec2-xxxA-1.compute.amazonaws.com closed.
Last login: Thu Jan 28 14:09:48 2016 from ip-172-xx-xx-01.compute.internal
[centos@ip-172-xx-xx-01 ~]$ 
因此,正如您所看到的,它告诉我它关闭了连接,但在关闭连接后,提示符再次显示主机的IP。 我必须键入
exit
,然后它会进一步转到下面的主机(我也必须退出)


在脚本中添加
exit
没有帮助。然后它会在第一个主机之后停止所有操作。

尝试从命令中删除
-tt
选项。

为什么有
-tt
选项,如果您只是尝试运行
hostname
并返回?稍后我将需要以root身份执行命令第一行
ip-…
来自何处?我有一个类似的命令,使用-t-t,它工作正常。为什么-tt会打破它?它不应该,但它可能会。检查答案。我知道您希望打印每个实例的主机名,然后保持连接以执行其他命令,对吗?如果是这种情况,您应该使用
${SSHKEY}$i“hostname;bash”