Unix 使用脚本在N个服务器之间进行SSH
我有n个服务器,如c0001.test.cloud.com、c0002.test.cloud.com、c0003.test.cloud.com,我想在这些服务器之间执行ssh,如: 从服务器:c0001执行到c0002的ssh,然后退出服务器。 返回c0001对c0003执行ssh,然后退出服务器 因此,通过这种方式,它将在运行时执行脚本,而无需输入任何输入,并且我们可以有n个服务器。 我写了一个剧本:Unix 使用脚本在N个服务器之间进行SSH,unix,ssh,ssh-keys,Unix,Ssh,Ssh Keys,我有n个服务器,如c0001.test.cloud.com、c0002.test.cloud.com、c0003.test.cloud.com,我想在这些服务器之间执行ssh,如: 从服务器:c0001执行到c0002的ssh,然后退出服务器。 返回c0001对c0003执行ssh,然后退出服务器 因此,通过这种方式,它将在运行时执行脚本,而无需输入任何输入,并且我们可以有n个服务器。 我写了一个剧本: str1=c0001.test.cloud.com,c0002.test.cloud.com
str1=c0001.test.cloud.com,c0002.test.cloud.com,c0003.test.cloud.com
string="$( cut -d ',' -f 2- <<< "$str1" )"
echo "$string"
for j in $(echo $string | sed "s/,/ /g")
do
ssh appAccount@j
done
str1=c001.test.cloud.com、c002.test.cloud.com、c003.test.cloud.com
string=“$(cut-d',”-f 2-假设要运行的命令数量很少,可以:
创建从c0001.test.cloud.com运行到每个服务器的命令脚本。例如,在本地计算机上创建一个名为commands.sh
的文件,其中包含:
hosts="c0002.test.cloud.com c0003.test.cloud.com"
for host in $hosts do
ssh -o StrictHostKeyChecking=no -q appAccount@$host <command 1> && <command 2>
done
但是,如果您的需求变得更加复杂,一个更健壮的解决方案可能是使用集群管理工具,例如,如果您想让它退出,您将需要ssh
来运行命令,甚至sshappAccount@j退出
但超出此范围后“将无法正常运行”“非常模糊,它到底是做了什么你不想做的事,还是做了什么你不想做的事?str1=c001.test.cloud.com,c002.test.cloud.com,c003.test.cloud.com string=“$(cut-d',”-f2-是,在脚本ssh中appAccount@j
应该是ssh appAccount@$j
ssh -o StrictHostKeyChecking=no -q appAccount@c0001.test.cloud.com 'bash -s' < commands.sh