ssh执行命令

#!/bin/bash

#-----------------------------------
#Author: panb
##此脚本用来向openresty服务器执行命令
#-----------------------------------

set -e
Usage(){
        echo "Usage: $0 host1 host2 ... 'command'"
}

if [ $# -lt 2 ];then
        Usage
        exit 0
else
        cmd=${!#}
fi


i=1
success=0
failed=0

for ip in $@;do
        if [ $i -eq $# ];then
		break
	fi
	ssh -i ops-ibox.pem root@$ip "$cmd" 
	if [ $? -eq 0 ];then
		success=$(($success+1))
		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-------------------------'
echo -e "\033[32msuccess: $success | failed: $failed \033[0m"
echo '-------------------------'

  

ssh执行命令

 

posted @ 2020-06-16 14:31  jcici  阅读(237)  评论(0编辑  收藏  举报