通过expect自动登录到30台linux机器运行相同命令

 

一、用以下expect脚本可以同时对200台机器执行同一个命令,

比如:打补丁(expect remote.exp)

#!/user/bin/expect -f

for {set i 1} {$i<=200} {incr i} {

spawn ssh -p 1022 -l root 192.168.6.$i

expect "*password: "

sleep 3

send "123456\r"

expect "username\r"

send "su -\r" #SSH普通用户名密码登录

expect "Password:"

send "123456\r" #SSH的root用户名密码登录

expect -timeout 1

expect "root"

#send "find / -name sambafeng.txt\r"

send "patch -R -p1 patching file prj0name\r"

#interact 中断调试

}

 

二、用ssh密钥方式

#!/bin/bash

#============author sambafeng===================

#==============INIT=============================

deployDir=/opt/fordeploy

backup=/opt/backup

scriptDir=/opt/script

#=============IP Partten=================================================

#url="$1"

#url_1=${url//\\//}

#url_2=${url_1#*94}

#dir="/opt/share"$url_2

#appname=$(ls -l ${dir}/|grep "\."|awk '{printf $9 "\n"}')

#cp $dir/$appname $deployDir

#=============eli the version====================

x=0

for filename in $deployDir/*;

do

     fullname=$(basename $filename)

     p_name=$(echo $fullname|awk -F "-[0-9]" '{print $1}'|awk -F "-stage2" '{print $1}'|awk -F "." '{print $1}')

     p_type=$(echo $fullname| rev | cut -c 1-3 | rev)

     PackageName="$p_name"."$p_type"

     mv -f $deployDir/$fullname $deployDir/$PackageName

     array[$x]=$PackageName

     let x+=1

done

#===============Remote deploy===================================================

echo "please select the package for deploy"

select var in ${array[@]}

do

 for n in $(cat /opt/script/map.properties |grep "${var}")

    do

       list=($(echo $n | awk -F',' '{printf("%s %s %s %s",$1,$2,$3,$4)}'))

          echo "==PackageName:${list[0]}==serverIp:${list[1]}==NodeName:${list[2]}==appName:${list[3]}========"

             echo -n "Are sure begin deploy?(Y/N)"

             read YES_OR_NO

             case $YES_OR_NO in

                  y|Y|yes|YES)

                  scp $deployDir/${list[0]} oracle@${list[1]}:$deployDir

                  ssh ${list[1]} "sh $scriptDir/redeploy.sh ${list[2]} ${list[3]} ${list[0]};exit;"

                  ;;

                  n|N|no|NO)

                  echo "canceled"

                  ;;

              *)

             echo "Error ,you should input[Y|N]"

             exit 1

              ;;

             esac

     done

         rm -f $deployDir/${list[0]}

         echo "$var deploy finished"

done

 

三、python实现ssh批量登陆执行命令 #!/usr/bin/env python import pexpect def ssh_cmd(ip, passwd, cmd): ret = -1 ssh = pexpect.spawn('ssh root@%s "%s"' % (ip, cmd)) try: i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5) if i == 0 : ssh.sendline(passwd) elif i == 1: ssh.sendline('yes\n') ssh.expect('password: ') ssh.sendline(passwd) ssh.sendline(cmd) r = ssh.read() print r ret = 0 except pexpect.EOF: print "EOF" ssh.close() ret = -1 except pexpect.TIMEOUT: print "TIMEOUT" ssh.close() ret = -2 return ret #!/usr/bin/python import paramiko import threading def ssh2(ip,username,passwd,cmd): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,22,username,passwd,timeout=5) for m in cmd: stdin, stdout, stderr = ssh.exec_command(m) # stdin.write("Y") #简单交互,输入 ‘Y’ out = stdout.readlines() #屏幕输出 for o in out: print o, print '%s\tOK\n'%(ip) ssh.close() except : print '%s\tError\n'%(ip) if __name__=='__main__': cmd = ['cal','echo hello!']#你要执行的命令列表 username = "" #用户名 passwd = "" #密码 threads = [] #多线程 print "Begin......" for i in range(1,254): ip = '192.168.1.'+str(i) a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd)) a.start()

你可能感兴趣的:(linux,F#)