标签:target 管理 实例 11.2 put 批量管理 stderr 允许 cache
简单主机批量管理工具
需求:
1.主机分组
2.主机信息使用配置文件
3.可批量执行命令、发送文件,结果实时返回
4.主机用户名密码、端口可以不同
5.执行远程命令使用paramiko模块
Host_manage/ #程序目录
├── bin #启动程序目录
│ └── start.py #启动程序文件
├── conf #配置目录
│ ├── __pycache__
│ │ └── settings.cpython-36.pyc
│ └── settings.py #配置文件
├── core #主程序目录
│ ├── __pycache__
│ │ └── src.cpython-36.pyc
│ └── src.py #主程序
└── log
bin/start.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 4 import os 5 import sys 6 7 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 8 sys.path.append(BASE_DIR) 9 print(BASE_DIR) 10 11 from core import src 12 13 if __name__ == ‘__main__‘: 14 src.run()
conf/settings.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 4 host_dic = { 5 "group1":{ #分组1 6 "h1":{"IP":"192.168.11.1", "username":"test001", "password":"abc", "port":22}, 7 "h2":{"IP":"192.168.11.2", "username":"test002", "password":"abc", "port":22}, 8 }, 9 "group2":{ #分组2 10 "h1":{"IP":"192.168.21.1", "username":"test003", "password":"abc", "port":22}, 11 "h2":{"IP":"192.168.21.2", "username":"test004", "password":"abc", "port":22}, 12 "h3":{"IP":"192.168.21.3", "username":"test005", "password":"abc", "port":22}, 13 }, 14 }
core/src.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 4 from conf import settings 5 import paramiko 6 import threading 7 import os 8 9 class Host_remote(): 10 ‘‘‘ 11 批量远程管理用户组主机 12 ‘‘‘ 13 #初始化 14 def __init__(self, host, port ,username, password, cmd): 15 self.host = host 16 self.port = port 17 self.username = username 18 self.password = password 19 self.cmd = cmd 20 21 def run(self): 22 ‘‘‘ 23 用进程 连接远程 主机后调用 24 :return: 25 ‘‘‘ 26 cmd_str = self.cmd.split()[0] 27 if hasattr(self, cmd_str): #反射 :调用put方法 28 getattr(self, cmd_str)() 29 else: 30 setattr(self, cmd_str, self.cmd) 31 getattr(self, cmd_str)() #调用cmd方法,执行批量命令处理 32 33 def cmd(self): 34 ‘‘‘ 35 批量命令处理 36 :return: 37 ‘‘‘ 38 ssh = paramiko.SSHClient() #创建ssh对象 39 #允许连接不在know_hosts文件中的主机 40 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 41 ssh.connect(hostname=self.host,port=self.port,username=self.username,password=self.password) 42 stdin,stdout,stderr = ssh.exec_cmd(self.cmd) 43 result = stdout.read() 44 print("%s".center(40, "-") % self.host) 45 print(result.decode()) 46 ssh.close() 47 48 def put(self): 49 ‘‘‘ 50 发送文件 51 :return: 52 ‘‘‘ 53 filename = self.cmd.split()[1] #要上传的文件 54 transport = paramiko.Transport((self.host, self.port)) 55 transport.connect(username=self.username, password=self.password) 56 sftp = paramiko.SFTPClient.from_transport(transport) 57 sftp.put(filename, filename) 58 print("put sucesss") 59 60 transport.close() 61 62 63 def show_host_list(): 64 ‘‘‘ 65 选择用户组 显示 主机名 与 IP 66 :return: 67 ‘‘‘ 68 for index, key in enumerate(settings.host_dic): 69 print("%s\033[34m 主机组:%s \033[0m \033[33m 主机数量:%s\033[0m" %(index + 1,key,len(settings.host_dic[key]))) 70 while True: 71 choose_host_list = input(">>(请输入用户组编号如:group1): ").strip() 72 host_dic = settings.host_dic.get(choose_host_list) 73 if host_dic: 74 for key in host_dic: 75 print(key, host_dic[key]["IP"]) 76 return host_dic 77 else: 78 print("不退出此组!") 79 80 81 def interactive(choose_host_list): 82 ‘‘‘ 83 根据选择的 用户组的 主机起 多个线程进行 批量交互 84 :param choose_host_list: 85 :return: 86 ‘‘‘ 87 thread_list = [] 88 while True: 89 cmd = input(">>: ").strip() 90 if cmd: 91 for key in choose_host_list: 92 host, port, username, password = choose_host_list[key]["IP"], choose_host_list[key]["port"], 93 choose_host_list[key]["username"], choose_host_listy[key]["password"] 94 func = Host_remote(host, port, username, password, cmd) # 实例化类 95 t = threading.Thread(target=func.run) # 起线程 96 t.start() 97 thread_list.append(t) 98 for t in thread_list: 99 t.join() # 主线程等待子线程执行完毕 100 else: 101 continue 102 103 104 def run(): 105 choose_host_list = show_host_list() 106 interactive(choose_host_list)
标签:target 管理 实例 11.2 put 批量管理 stderr 允许 cache
原文地址:http://www.cnblogs.com/zhuzhiwen/p/7854276.html