python-python作业-简单主机批量管理工具
题目:简单主机批量管理工具
需求:
- 主机分组
- 登录后显示主机分组,选择分组后查看主机列表
- 用多线程可批量执行命令、发送文件,结果实时返回
- 主机用户名密码可以不同
主程序:
import json,paramiko,threading class option(object): def __init__(self,ip_addr,username,password,port,cmd): self.ip_addr=ip_addr self.username=username self.password=password self.port=port self.cmd=cmd def ssh(self): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=self.ip_addr, port=self.port, username=self.username, password=self.password) stdin, stdout, stderr = ssh.exec_command(self.cmd) res, err = stdout.read(), stderr.read() if res: result = res else: result = err print("----- host %s -----"%self.ip_addr) print(result.decode()) ssh.close() def ftp(self): try: transport = paramiko.Transport((self.ip_addr, self.port)) transport.connect(username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(self.cmd.split()[1], self.cmd.split()[2]) transport.close() print("----- host %s"%self.ip_addr) print("upload done") except Exception as e: print(e) print("error,please check if the path is vailed or if the filename is exist") self.ftphelp() def ftphelp(self): print("put /s_path/s_file_name /d_path/d_file_name") print("d_path default is /root/") with open("database.json","r") as f: data=f.read() data_d=json.loads(data) while True: print("----- groups -----") for group in data_d: #打印组 print(group) chose_group=input("chose group:") if data_d.get(chose_group): ip_dict=data_d[chose_group] print("----- ip_addr -----") for ip in ip_dict: print(ip) elif chose_group=="q": exit() else: print("no this group.") continue while True: chose_cmd_type=input('''----- chose option type ----- 1.ssh 2.ftp 3.back 4.quit Your chose num:''') if chose_cmd_type=="1": while True: chose_cmd=input(">>:") if chose_cmd=="b": break elif chose_cmd=="q": exit() else: thread_list=[] for ip_addr in ip_dict: # print(ip_dict) username,password,port=ip_dict[ip_addr]["username"],ip_dict[ip_addr]["password"],ip_dict[ip_addr]["port"] opt=option(ip_addr,username,password,port,chose_cmd) #实例化类 t = threading.Thread(target=opt.ssh) t.start() thread_list.append(t) for i in thread_list: i.join() elif chose_cmd_type=="2": while True: chose_cmd = input(">>:") if chose_cmd=="b": break elif chose_cmd=="q": exit() else: thread_list = [] for ip_addr in ip_dict: username, password, port = ip_dict[ip_addr]["username"], ip_dict[ip_addr]["password"], ip_dict[ip_addr]["port"] opt = option(ip_addr, username, password, port, chose_cmd) # 实例化类 t = threading.Thread(target=opt.ftp) t.start() thread_list.append(t) for i in thread_list: i.join() elif chose_cmd_type=="3": break elif chose_cmd_type=="4": exit() else: print("The num is not exist")
数据库:
{"group1": {"192.168.242.129": {"password": "225325", "username": "root", "port": 22}, "192.168.242.132": {"password": "225325", "username": "root", "port": 22}}, "group2": {"192.168.242.130": {"password": "225325", "username": "root", "port": 22},"192.168.242.131": {"password": "225325", "username": "root", "port": 22}}}