def ssh_get_connect(self,retryList): #允许连接不在know_hosts文件中的主机。 self.sh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #用原始密码连接做第一次尝试 try: self.sh.connect(self.ip, self.port, self.userName, self.passWord,timeout=3) self.conStatus=True except Exception as e: err=str(e) if 'Authentication failed' in err: print(u'身份验证失败,重试密码中......') elif err: loggerForSsh.error(u"%s连接超时,目标主机未开或网络出现问题"%self.ip) #若出现这样的状况直接退出 return 0 #若第一次尝试失败,进行密码重试 if self.conStatus is False: for rePass in retryList: try: self.sh.connect(self.ip, self.port, self.userName, rePass) self.conStatus=True #更新配置文件中的password self.passWord = rePass wd = reader() wd.alterIp(self.ip,rePass) break except Exception as e: err2=str(e) if err2 != '': continue #若尝试完所有密码还是连接失败 if self.conStatus is False: loggerForSsh.error(u'%s试完所有密码未能连接成功'%self.ip)
def get_pid(self): """ 输入:无 输出:pid(得到了pid); None(未得到pid) 功能:获取pid文件中的进程号 """ try: with open(self.file,"r") as f: pid = f.read() return pid except IOError as e: loggerForSsh.error(u"读取pid文件失败") return None
def write_pid(self): """ 输入:无 输出:布尔值,是否已经写入进程号 功能:给pid文件写入进程号 """ try: with open(self.file,"w") as f: f.write(str(os.getpid())) return True except IOError as e: loggerForSsh.error(u"写入pid文件失败") return False
def run(): THREAD_SUM = int(os.environ["THREAD_SUM"]) wd=reader() wt=writer() connect_info=wd.readIp() #线程数为0的情况,默认改成1,但是config文件中不予改变 if THREAD_SUM <= 0: loggerForSsh.warn(u"配置文件(config.py)中线程数(THREAD_SUM)应当大于0,否则执行时默认为1") THREAD_SUM = 1 distribution = distribute(len(connect_info), THREAD_SUM) cmds=wd.readCmd() retry = wd.readRetry() ssh_showResult = [] threads = [] # 添加线程到线程列表 for thread in range(THREAD_SUM): if distribution[thread] is None: #当出现None时,说明线程数量比服务器数量多,多余的线程可以开启 break servers = [connect_info[i] for i in distribution[thread]] threads.append(myThread(servers,retry,cmds,thread)) # 开启新线程 thread_sum = range(len(threads)) for thread in thread_sum: threads[thread].setDaemon(True) threads[thread].start() # 等待所有线程完成 for thread in thread_sum: threads[thread].join() loggerForSsh.info(u"所有的线程执行完毕") #得到所有的结果 for thread in thread_sum: ssh_showResult += threads[thread].get_result() try: wt.csv_write_head() for p in ssh_showResult: wt.csv_write_body(p) except IOError as e: loggerForSsh.error(u"操作csv文件失败")
def ssh_get_connect(self, retryList): #允许连接不在know_hosts文件中的主机。 self.sh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #用原始密码连接做第一次尝试 try: self.sh.connect(self.ip, self.port, self.userName, self.passWord, timeout=3) self.conStatus = True except Exception as e: err = str(e) if 'Authentication failed' in err: print('身份验证失败,重试密码中......') elif '[WinError 10060]' in err: #print('连接超时无答复,'+self.ip+'主机未开或网络出现问题')#需要特别反应 loggerForSsh.error("%s连接超时,目标主机未开或网络出现问题" % self.ip) #若第一次尝试失败,进行密码重试 if self.conStatus is False: for rePass in retryList: try: self.sh.connect(self.ip, self.port, self.userName, rePass) self.conStatus = True #更新配置文件中的password wt = writer() wt.updete_json(ip=self.ip, password=rePass, passAlter=True) break except Exception as e: err2 = str(e) if err2 != '': continue #若尝试完所有密码还是连接失败 if self.conStatus is False: loggerForSsh.error('%s试完所有密码未能连接成功' % self.ip)