国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

批量遠程執行linux服務器程序--基于pxpect(多進程、記日志版)

2019-11-14 16:54:40
字體:
來源:轉載
供稿:網友
#!/usr/bin/python

'''
Created on 2015-06-09
@author: Administrator
'''


import pexpectimport os,sysfrom optparse import OptionParserimport logging,multiprocessingimport logging.handlers#menueusage='%PRog [-h][-s Servers][-c CMDS][--version]'parser=OptionParser(usage=usage,version='Hu 2.0_20150609')parser.add_option('-s','--Server',dest='server',default='ip.txt',help='The Server Info')parser.add_option('-c','--CMDS',dest='cmd',default='pwd',help='You wann to execute commands')(options,args)=parser.parse_args()print options.server,options.cmdlogger=logging.getLogger()logger.setLevel(logging.DEBUG)ch=logging.StreamHandler()cc=logging.handlers.RotatingFileHandler('log.txt', maxBytes = 1024*1024, backupCount = 5)formatter=logging.Formatter("%(asctime)s-%(name)s-%(levelname)s %(message)s")ch.setFormatter(formatter)cc.setFormatter(formatter)logger.addHandler(ch)logger.addHandler(cc)#ssh functionsdef connect(ip,username,passWord,port,prompt=']#'): try: ssh_newkey='Are you sure you want to continue connecting' child=pexpect.spawn('ssh '+username + '@'+ip+' -p '+port,maxread=5000) child.logfile=fout i=child.expect([prompt,'assword:*',ssh_newkey,'refused',pexpect.TIMEOUT,'key.*? failed']) print i #if not False: # print child.before,child.after if i==0: pass elif i==1: print password child.sendline(password) elif i==2: child.sendline('yes') elif i==4: raise Exception('Error TIMEOUT!') elif i==3: print 'Connect refused' elif i==5: print child.before,child.after os.remove(os.path.expanduser('~')+'/.ssh/known_hosts') child.expect('#') print child.after,child.before child.sendline(options.cmd) child.expect('#') print child.before,child.after print 'The command %s result is:' % options.cmd print child.before except Exception,e: logger.error("Connect Error") #check -s isn't exitsif os.path.exists(options.server): filename=options.server passelse: print 'Please check %s and ip.txt is exits' % options.server exit(-1)#executefout=file('mylog.txt','w')for line in open(filename): ip,user,passwd,port=line.strip().split() print '*'*50 print 'The follow ip is %s:' % ip p=multiprocessing.Pool(processes=4) result=p.apply_async(connect,[ip,user,passwd,port]) print result.get() #connect(ip,user,passwd,port)

E-mail:362299908@QQ.com 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和静县| 弥勒县| 陆良县| 徐闻县| 西和县| 辉县市| 乌鲁木齐县| 南宫市| 修武县| 桦川县| 通榆县| 杭锦旗| 定襄县| 鄂托克旗| 盐池县| 敦煌市| 茌平县| 通辽市| 克东县| 福贡县| 青浦区| 普陀区| 开原市| 于都县| 兴安县| 若尔盖县| 吉木萨尔县| 孟州市| 麻阳| 边坝县| 上犹县| 阜康市| 织金县| 化州市| 大化| 芮城县| 如皋市| 沙田区| 合水县| 沾化县| 体育|