paramiko+threading批量检查系统状态
发布时间:2020-12-17 17:26:35 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/env python#coding:utf-8import paramikoimport threadingimport os,sys,timedef ssh2(ip,pt,pw,us,comm): try: ssh = paramiko.SSHClient
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #!/usr/bin/env python #coding:utf-8 import paramiko import threading import os,sys,time def ssh2(ip,pt,pw,us,comm): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,port=pt,username=us,password=pw) f.write(('='*20 +'%s status' +'='*20 + 'n')% ip) for COMM in comm: stdin,stdout,stderr = ssh.exec_command(COMM) read = stdout.read() f.write(read) ssh.close() # except SyntaxError: # print "please checking" # sys.exit() except: print "Connection refused" sys.exit() if __name__ == '__main__': print " 33[1;31m 请等待..... 33[0m" open_ip = open('/home/test/iplist.txt') f = open('/home/test/Allsystem.log','w') paramiko.util.log_to_file('/home/test/paramiko.log') up='uptime' df = "df -Ph |awk 'NR == 1 || NR == 6{print $0}'" net="ifconfig |awk 'NR==8'|sed 's/^s*//g'" command = [up,df,net] for ip in open_ip.readlines(): split = ip.split() name = split[1] ipaddr = split[2] password = split[3] aa = threading.Thread(target=ssh2,args=(ipaddr,22,password,name,command)) aa.start() aa.join() open_ip.close() f.write('='*20 +'All done' +'='*20 + 'n') f.flush() f.close() 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |