def alter_pass(self, js, ip, password): for i in js['server']: if i['ip'] == ip: i['password'] = password loggerForTelnet.info("修改%s的正确密码为%s" % (ip, password)) loggerForSsh.info("修改%s的正确密码为%s" % (ip, password)) return json.dumps(js)
def _ip_del(self, js, ip): tmp = js['server'].copy() for i in tmp: if i['ip'] == ip: js['server'].remove(i) loggerForTelnet.info("删除服务器%s" % ip) loggerForSsh.info("删除服务器%s" % ip) return json.dumps(js)
def _retry_del(self, js, ip, delt): for i in js['server']: if i['ip'] == ip: for j in i['retry']: if j == delt: i['retry'].remove(j) loggerForTelnet.info("删除%s的重试密码%s" % (ip, delt)) loggerForSsh.info("删除%s的重试密码%s" % (ip, delt)) return json.dumps(js)
def _retry_add(self, js, ip, array): for i in js['server']: if i['ip'] == ip: i['retry'] += array # print(i.retry) # js["retry_set"] = list(set(js["retry_set"])) loggerForTelnet.info("增加%s的重试密码%s" % (ip, array)) loggerForSsh.info("增加%s的重试密码%s" % (ip, array)) return json.dumps(js)
def alterIp(self, ip, password): f = open(self.ip_file, "r+") lines = f.readlines() f.close() with open(self.ip_file, "w") as f_w: for line in lines: if ip in line: oldPassword = line.split()[2] newPassword = '******' % password line = line.replace(oldPassword, newPassword) f_w.write(line) f_w.close() loggerForSsh.info("修改%s的正确密码为%s" % (ip, password))
def kill(self): """ 输入:无 输出:返回布尔值,若成功为True,反之False 功能:执行系统命令 """ pid = self.get_pid() if pid is None: return False s = os.popen("taskkill /F /pid %s"%self.get_pid()) if len(s.read()) > 0: loggerForSsh.info(s.read()) return True else: loggerForSsh.info(u"杀死进程失败,进程不存在") 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文件失败")
print('ok') 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,
def _ip_add(self, js, ip, user, password): tmp = {"ip": ip, "password": password, "user": user, "retry": []} js['server'].append(tmp) loggerForTelnet.info("增加服务器%s" % ip) loggerForSsh.info("增加服务器%s" % ip) return json.dumps(js)