ssh 批量执行

#!/bin/bash
#creat by zhouwj
#2020.03.24
function scan(){
if [[ $watchremain -ge $needtimes ]] 
then
        DAY=$(date +%Y-%m-%d)
        PWD=/tmp/$DAY/$value/
        /bin/mkdir -p $PWD
        if test -z "$PWD";then 
           exit 
        else 
           rm -f $PWD*
        fi 
        #CMD='ls -l /var/ipdb/cfg/out-cover'            #为需要操作的命令
        num=0
        for i in `cat /tmp/iplist`
        do
                {
                `ssh -o ConnectTimeout=5 -o NumberOfPasswordPrompts=0  watch@${i} /bin/bash <$CMD >$PWD/${i} 2>/dev/null `
                }&
        
                let num=$num+1
                completions=$(echo "$num $needtimes" | awk '{printf("%0.1f\n",$1/$2*100)}')
                echo  -ne "扫描进度:$completions%\r"
        done

        wait 
        echo "扫描结束"
        echo "扫描文件存储路径: $PWD"

else
        echo "Your watch times are not enough :$watchremain "
        exit 
fi
}

main(){
>/tmp/iplist
if [[ $# -eq 3 ]];then
        case $1 in
                appgroup)
                               
                appservice)
                               
                apptag)
                               
                dhc)
                                
				filename)
                                $(cat $2>/tmp/iplist);;
                *)
                                echo "Request with error params !  参数1:类型支持[appservice,appgroup,apptag,dhc,filename]  "
                                                exit
                esac
        #opremain=`sshuser -m query |grep "op" |awk '{print $6}'`
        watchremain=`sshuser -m query |grep "watch" |awk '{print $6}'`
        #rootremain=`sshuser -m query |grep "root" |awk '{print $6}'`
        needtimes=`cat /tmp/iplist|wc -l`
        CMD=$3
        value=$2
        scan
else
        echo "Request with error params ! 参数1:类型支持[appservice,appgroup,apptag,dhc,filename] 参数2:具体参数值,可接文件名  参数3 :执行脚本命令/tmp/task.sh"
fi
}

main $@

你可能感兴趣的:(ssh 批量执行)