使用paramiko,pymysql批量管理主机, 用户信息,及主机信息存在数据库中 例子: #!/usr/bin/python # --*-- coding:utf-8 --*-- import paramiko import pymysql from threading import Thread, RLock import sys def connect_user_tb(): """返回执行sql结果""" conn = pymysql.Connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘aixocm‘, database=‘oldboy‘, charset=‘utf8‘) cursor = conn.cursor() cursor.execute(‘select user_tb.id,username,passwd,email,deptment_tb.name from user_tb left join deptment_tb on user_tb.deptId=deptment_tb.id‘) db_res = cursor.fetchall() cursor.close() conn.close() return db_res def auth(): """用户登录""" count = 0 while count < 3: username = input("请输入用户名: ") passwd = input("请输入密码: ") user_info = connect_user_tb() for i in user_info: if username == i[1] and passwd == i[2]: return (i) else: count += 1 print(‘您输入的用户名密码不正确,请重新输入‘) continue else: return False def connect_host_user_tb(data): conn = pymysql.Connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘aixocm‘, database=‘oldboy‘, charset=‘utf8‘) cursor = conn.cursor() cursor.execute(‘select host_tb.ip,host_tb.port,host_tb.username,host_tb.passwd from host_user_tb inner join host_tb on host_user_tb.hostId=host_tb.id and host_user_tb.userId=%s‘,[data]) host_info = cursor.fetchall() cursor.close() conn.close() return host_info class Mythread(Thread): def __init__(self, host, port, username, passwd, lock, cmd): super().__init__() self.host = host self.port = port self.username = username self.passwd = passwd self.lock = lock self.cmd = cmd def run(self): with self.lock: try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) # 忽略密钥认证,使用密码认证 ssh.connect(hostname=self.host, username=self.username, password=self.passwd) stdin, stdout, stderr = ssh.exec_command(self.cmd) print("#######################主机%s开始执行名########################" % (self.host)) print(stdout.read().decode(‘utf-8‘)) print(stderr.read().decode(‘utf-8‘)) ssh.close() print("#######################主机执行结束########################") except Exception as e: print(e) def main(): """程序入口""" lock = RLock() auth_res = auth() if auth_res: host_info = connect_host_user_tb(auth_res[0]) str = "用户名: %s 邮 箱: %s 部 门: %s" %(auth_res[1],auth_res[3],auth_res[4]) print(str) print(‘ip port username passwd‘) for host in host_info: print(host[0] + ‘ ‘,host[1], ‘ ‘ + host[2] + ‘ ‘ + host[3]) t_list = [] while True: try: select_ip = input("请输入要执行命令的主机ip,多个主机以逗号分隔,未选择表示执行所有主机:>>").strip() ip_list = select_ip.split(‘,‘) cmd = input("请输入执行的命令>>: ").strip() if not select_ip: for host in host_info: t = Mythread(host[0],host[1],host[2],host[3],lock,cmd) t.daemon=True t.start() t_list.append(t) for t in t_list: t.join() else: for ip in ip_list: for host in host_info: if ip == host[0]: t = Mythread(host[0], host[1], host[2], host[3], lock, cmd) t.daemon = True t.start() t_list.append(t) for t in t_list: t.join() is_exit = input("是否退出,输入yY/nN") if is_exit.upper() == ‘Y‘: break except Exception as e: print(e) break else: print(‘登录失败,退出程序‘) if __name__ == ‘__main__‘: main()
时间: 2024-10-29 05:07:54