#11.vssh: 在对单台机器做操作时我们经常会用“ssh ip”的方式登录到一台服务器上,能不能编写这样一个工具vssh ip1,ip2,...ipn 来模拟登录到n 台服务器,登录后所有操作相当于同时对n 台服务器生效。
##Answer
执行此脚本前,需要先进行公钥认证,将本机的公钥复制到目标主机上:
# ssh-copy-id -i ~/.ssh/id_rsa.pub remote_host
###shell 代码:
#!/bin/bash
#
cmd=${!#}
logfile=$(mktemp)
i=1
success=0
failed=0
for ip in $@;do
if [ $i -eq $# ];then
break
fi
ssh $ip $cmd &> $logfile
if [ $? -eq 0 ];then
((success++))
echo -e "\n\033[32m $ip | success >> \033[0m \n"
cat $logfile
else
((failed++))
echo -e "\n\033[31m $ip | failed >> \033[0m\n "
cat $logfile
fi
((i++))
done
echo -e "\n\033[32m success: $success || failed: $failed \033[0m"
运行结果: