1.
1 # -*- coding:utf-8 -*- 2 3 import os,sys,paramiko,time 4 #*******判断所有测试环境是否有某个文件,*********** 5 s7 = "10.10.123.96" 6 s3 = "10.10.39.70" 7 host_dic = {"10.10.154.77":"s1", 8 "10.10.83.96":"s2", 9 "10.10.39.70":"s3", 10 "10.10.46.120":"s4", 11 "10.10.15.128":"s5", 12 "10.10.84.48":"s6", 13 "10.10.123.96":"s7", 14 "10.10.105.91":"qa1", 15 "10.10.92.48":"qa2", 16 "10.10.50.30":"qa3", 17 "10.10.40.136":"qa4", 18 "10.10.189.139":"qa5" 19 } 20 exist ={} 21 not_exist = {} 22 port =22 23 user = "root" 24 passwd = "B^Dc%4LSBvhZZK3B" 25 26 def ssh_cmd(ip,port,cmd,user,passwd): 27 result = "" 28 try: 29 ssh = paramiko.SSHClient() 30 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 31 ssh.connect(ip,port,user,passwd) 32 stdin, stdout, stderr = ssh.exec_command(cmd) 33 result = stdout.read() 34 print result 35 ssh.close() 36 except: 37 print "ssh_cmd err." 38 return result 39 while True: 40 raw = raw_input("按a批量执行命令: 按b批量判断文件是否存在: 按q退出:") 41 if raw =="a": 42 cmd = raw_input("请输入要执行的命令:") 43 for i in host_dic: 44 print host_dic.get(i) 45 ssh_cmd(i,port,cmd, user,passwd) 46 elif raw =="b": 47 while True: 48 print "