python自学之路之-简单主机批量管理工具实现
一、项目需求
1. 实现主机分组 2. 选择分组后查看主机列表 3. 可批量执行命令、发送文件、下载文件,结果实时返回
二、源码
1. 目录结构
test_host.py 为上传下载测试文件
2. settings.py(主机信息,字典形式存储)
host_dict={ 'dev':{ "host1":{'IP':'192.168.10.182','hostname':'dev-01','port':22,'user':'root','password':'root'}, "host2":{'IP':'192.168.10.170','hostname':'dev-02','port':22,'user':'root','password':'root'}, "host3":{'IP':'192.168.10.170','hostname':'dev-03','port':22,'user':'root','password':'root'}, }, 'test':{ "host1":{'IP':'47.52.77.190','hostname':'test-01','port':22,'user':'root','password':'testtest'}, "host2":{'IP':'47.243.45.24','hostname':'test-02','port':22,'user':'root','password':'testtest'}, "host3":{'IP':'47.90.156.40','hostname':'test-03','port':22,'user':'root','password':'testtest'}, }, 'pro': { "host1": {'IP': '47.90.10.144','hostname':'pro-03', 'port': 22, 'user': 'root', 'password': 'test'}, "host2": {'IP': '47.52.3.190','hostname':'pro-03', 'port': 22, 'user': 'root', 'password': 'test'}, "host3": {'IP': '47.51.39.22','hostname':'pro-03', 'port': 22, 'user': 'root', 'password': 'test'}, "host4": {'IP': '149.128.90.25','hostname':'pro-03', 'port': 22, 'user': 'root', 'password': 'test'}, }, }
3. core.py(核心代码,被调用)
import settings import paramiko,threading
class Operate_Remote_Host(object): def __init__(self,host,port,username,password,cmd): self.host=host self.port=port self.username=username self.password=password self.cmd=cmd def run(self): str_cmd=self.cmd.split()[0]if hasattr(self,str_cmd): getattr(self, str_cmd)() else: setattr(self,str_cmd,self.command) getattr(self,str_cmd)() def command(self): ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) ssh.connect(hostname=self.host, port=self.port, username=self.username, password=self.password) stdin, stdout, stderr = ssh.exec_command(self.cmd) res, err = stdout.read(), stderr.read() result = res if res else err print(result.decode()) ssh.close() def put(self): src_file = self.cmd.split()[1] dest_file = self.cmd.split()[2] transport = paramiko.Transport(self.host, self.port) transport.connect(username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(src_file,dest_file) transport.close() def get(self): src_file = self.cmd.split()[1] dest_file = self.cmd.split()[2] transport = paramiko.Transport(self.host, self.port) transport.connect(username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.get(src_file,dest_file) transport.close() def show_host_list(): for key in settings.host_dict.keys(): print("环境类型:%s 主机数:%d" %(key, len(settings.host_dict[key]))) while True: """通过选择分组显示主机名与IP""" choose_env=input("请输入操作的环境>>>:").strip() if choose_env in settings.host_dict.keys(): host_show = settings.host_dict[choose_env] for host in host_show.keys(): print("IP:%s hostname:%s"%(host_show[host]['IP'],host_show[host]['hostname'])) return host_show else: print("NO exit this env!") def main(choose_host_dict): thread_list = [] while True: cmd=input("请输入命令>>>").strip() if cmd: for host in choose_host_dict: host,port,username,password=choose_host_dict[host]['IP'],choose_host_dict[host]['port'],choose_host_dict[host]['user'],choose_host_dict[host]['password'] func=Operate_Remote_Host(host,port,username,password,cmd) t = threading.Thread(target=func.run) t.start() thread_list.append(t) for t in thread_list: t.join() else: continue def run(): choose_host_dict=show_host_list() main(choose_host_dict)
4. main.py(程序入口)
import core if __name__ == "__main__": core.run()