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 err: loggerForSsh.error("%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('%s试完所有密码未能连接成功' % self.ip)
sshCon = sshConnecter(self.ip, self.username, self.password) sshCon.ssh_get_connect(self.retry) self.result.append(sshCon.passWord) if sshCon.conStatus == True: #print(self.ip+':') tmp = sshCon.ssh_do(cmds) for info in tmp: self.result.append(info) def get_result(self): return self.result if __name__ == '__main__': loggerForSsh.info("查看一次服务器信息") wd = reader() wt = writer() connect_info = wd.readIp() cmds = wd.readCmd() retry = wd.readRetry() ssh_showResult = [] threads = [] count = range(len(connect_info)) # 添加线程到线程列表 for j in connect_info: #print(j['ip'],j['user'],j['password'],retry) threads.append(myThread(j['ip'], j['user'], j['password'], retry, cmds))