paramiko類Fabric主機管理
阿新 • • 發佈:2018-01-02
hide 端口 用法 def com 信息 stdin tran try
環境:Linux python3.5
要求:
類 Fabric 主機管理程序開發:
1. 運行程序列出主機組或者主機列表
2. 選擇指定主機或主機組
3. 選擇讓主機或者主機組執行命令或者向其傳輸文件(上傳/下載)
4. 充分使用多線程或多進程
5. 不同主機的用戶名密碼、端口可以不同
結構:
bin-----|
start.py ......啟動目錄,需配置主機列表
core----|
main.py ......主目錄
conf----|
config.py ......配置目錄
system.ini ......配置文件
file ......上傳下載默認目錄
用法:
先在system.ini中配置主機IP組
選擇主機IP,輸入用戶名密碼,登陸
選擇需要執行命令或者傳輸文件
傳輸文件默認目錄為file目錄
bin:
#!/usr/bin/env python # -*-coding:utf-8-*- # Author:zh import os import sys import threading PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(PATH) from core import main from conf import config if __name__ == ‘__main__‘: conf = config.Configuration() ip_liststart.py= eval(conf.get_config()[0][1]) thread = threading.Thread(target=main.main, args=(ip_list,)) thread.start()
core:
#!/usr/bin/env python # -*-coding:utf-8-*- # Author:zh import paramiko import os PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+os.sep+"file"+os.sepmain.pyclass Fabric(object): ‘‘‘這個類用來初始化SFTPClient和SSHClient‘‘‘ def __init__(self, hostname, port, username, password): self.hostname = hostname self.port = port self.username = username self.password = password self.transport = paramiko.Transport((self.hostname, self.port)) self.transport.connect(username=self.username, password=self.password) def command(self, cmd_input): # 執行命令 ssh = paramiko.SSHClient() ssh._transport = self.transport stdin, stdout, stderr = ssh.exec_command(cmd_input) res, err = stdout.read(), stderr.read() result = res if res else err print(result.decode()) def sftp(self, data): # 上傳下載入口 self.sftp = paramiko.SFTPClient.from_transport(self.transport) data_split = data.split() sign = data_split[0] if len(data_split) == 2: file_path = data_split[1] if file_path.rfind("/") == -1: file_name = file_path else: file_name = file_path[file_path.rfind("/")+1:] local_path = PATH+file_name elif len(data_split) == 3: if sign == "get": local_path = data_split[2] file_path = data_split[1] if sign == "put": file_path = data_split[2] local_path = data_split[1] else: local_path = ‘‘ file_path = ‘‘ if hasattr(self, sign): func = getattr(self, sign) try: func(file_path, local_path) except Exception as e: print("錯誤 %s" % e) else: print("輸入錯誤") self.help() def get(self, *args): # 下載 file_path = args[0] local_path = args[1] self.sftp.get(file_path, local_path) def put(self, *args): # 上傳 file_path = args[0] local_path = args[1] self.sftp.put(local_path, file_path) @staticmethod def help(self): # 展示幫助信息 show = ‘‘‘ get path local_path ......從path下下載到本地目錄 put local_path path ......從本地上傳到path目錄下 ‘‘‘ print(show) def close(self): # 關閉連接 self.transport.close() def logon(): # 登陸,輸入端口,用戶名,密碼 while True: port = input("請輸入端口:") if not port: continue if not port.isdigit(): print("請輸入正確的端口") continue port = int(port) name = input("請輸入用戶名:") if not name: continue pwd = input("請輸入密碼:") if not pwd: continue return [port, name, pwd] def show(show_list): ‘‘‘展示列表給用戶選擇,並返回選擇信息‘‘‘ while True: for value, comment in enumerate(show_list): print("%s. %s" % (value+1, comment)) choose = input("請選擇:") if not choose: continue if choose == "exit": exit() try: choose = int(choose)-1 show_list[choose] return choose except (ValueError, IndexError) as e: print("輸入錯誤:%s ,請重新輸入" % e) continue def main(ip_list): # 主方法,程序入口 ip_list = ip_list choose_ip = show(ip_list) value_list = logon() port = value_list[0] name = value_list[1] pwd = value_list[2] while True: action_list = ["執行命令", "上傳下載"] choose_action = show(action_list) ssh = Fabric(ip_list[choose_ip], port, name, pwd) if choose_action == 0: func = ssh.command else: func = ssh.sftp while True: cmd_input = input("-->") if not cmd_input: continue if cmd_input == ‘b‘: ssh.close() break if cmd_input == ‘exit‘: exit() func(cmd_input)
conf:
#!/usr/bin/env python # -*-coding:utf-8-*- # _author_=zh import os import configparser PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) class Configuration(object): def __init__(self): self.config = configparser.ConfigParser() self.name = PATH+os.sep+"conf"+os.sep+"system.ini" def init_config(self): # 初始化配置文件,ip :客戶端IP,port:客戶端端口 if not os.path.exists(self.name): self.config["config"] = {"ip_list":[‘192.168.200.128‘, ‘192.168.200.129‘, ‘192.168.200.130‘]} self.config.write(open(self.name, "w", encoding="utf-8", )) def get_config(self, head="config"): ‘‘‘ 獲取配置文件數據 :param head: 配置文件的section,默認取初始化文件config的數據 :return:返回head中的所有數據(列表) ‘‘‘ self.init_config() # 取文件數據之前生成配置文件 self.config.read(self.name, encoding="utf-8") if self.config.has_section(head): section=self.config.sections() return self.config.items(section[0])config.py
paramiko類Fabric主機管理