码迷,mamicode.com
首页 > 其他好文 > 详细

简单主机批量管理工具

时间:2020-05-01 14:31:24      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:div   sof   out   image   insert   简单   space   for   get   

 

技术图片

技术图片

 

start.py

import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,BASE_DIR)#将库路径添加到path中好调用
print(BASE_DIR)
from core import more_server
if __name__ == ‘__main__‘:
    more_server.run()

settings.py

host_dic = {
    "group1": {  # 分组1
        "h1": {"IP": "192.168.11.1", "username": "test001", "password": "abc", "port": 22},
        "h2": {"IP": "192.168.11.2", "username": "test002", "password": "abc", "port": 22},
    },
    "group2": {  # 分组2
        "h1": {"IP": "192.168.21.1", "username": "test003", "password": "abc", "port": 22},
        "h2": {"IP": "192.168.21.2", "username": "test004", "password": "abc", "port": 22},
        "h3": {"IP": "192.168.21.3", "username": "test005", "password": "abc", "port": 22},
    },
}

server_connect.py

import paramiko,threading
from conf import settings
class server_recv():
    def __init__(self,host,part,usename,password,cmd):
        self.host = host
        self.part = part
        self.usename = usename
        self.password = password
        self.cmd = cmd
    def run(self):#调用put()和cmd()
        cmd_str = self.cmd.split()[0]
        if hasattr(self,cmd_str):
            getattr(self,cmd_str)()
        else:#处理命令
            setattr(self,cmd_str,self.cmd)#cmd_str = self.cmd
            getattr(self,cmd_str)()#调用cmd函数
    def put(self):
        filename = self.cmd.split()[1]
        transport = paramiko.Transport((self.host,self.part))
        transport.connect(self.usename,self.password)
        sftp = paramiko.SFTPClient.from_transport(transport)#建立一个引导的通道
        sftp.put(filename,filename)#本地地址,服务器地址
        print(‘put success‘)
        sftp.close()
    def get(self):
        filename = self.cmd.split()[1]
        transport = paramiko.Transport((self.host,self.part))
        transport.connect(self.usename,self.password)
        sftp = paramiko.SFTPClient.from_transport(transport)#建立一个引导的通道
        sftp.get(filename,filename)#本地地址,服务器地址
        print(‘get success‘)
        sftp.close()
    def cmd(self):
        ssh = paramiko.SSHClient()#创建一个 客户端实例
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#自动添加地址钥到known_host不出现yes选项
        ssh.connect(hostname=self.host,port=self.part,username=self.usename,password=self.password)#查看看需要填入什么
        sendin,sendout,senderro = ssh.exec_command(self.cmd)
        res,err = sendout.read(),senderro.read()
        result = res if res else err
        print(result)

def show_host_list():#主程序
    for index,key in enumerate(settings.host_dic):
        print(‘%s 主机组%s 主机数%s‘%(index,key,len(settings.host_dic[key])))
    while True:
        choose_host_list = input(‘>>:‘).split()
        host_dic = settings.host_dic(choose_host_list)
        if host_dic:
            for key in host_dic:
                print(key,host_dic["IP"])
            return host_dic
        else:
            print(‘请重新输入‘)
            continue

def interactive(choose_host_list):
    thread_list=[]
    while True:
        cmd = input(‘>>:‘)
        if cmd:
            for key in choose_host_list:
                host,part,usename,password=choose_host_list[key]["IP"],choose_host_list[key]["part"],choose_host_list[key]["usename"],choose_host_list[key]["password"]
                func = server_recv(host,part,usename,password)#实例化主类
                t = threading.Thread(target=func.run())#创建线程
                t.start()
                thread_list.append(t)
            for t in thread_list:
                t.join()
        else:
            print(‘输入错误‘)
            continue

def run():
    choose_host_list = show_host_list()#choose_host_list名字改成host_dic更好一点
    interactive(choose_host_list)

log

1.主机分组用字典做,客户机输入ip地址:主机名:账号:密码存入配置文件
2.接收正确的账号密码之后显示分组,分组之中可以查看主机名字
3.paramiko运行命令发送命令,用线程做出一个新的线程
 
运行:将屏幕的词进行运行,调用屏幕的字并且将其输入到文件处理的代码中
接收屏幕的字
选择相应的分组
产生线程并且连接
 
3.先创建一个实例
ssh = paramiko.SSHClient()
自动添加地址钥到known_host不出现yes选项
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
连接实例
ssh.connect(hostname=self.host,port=self.part,username=self.usename,password=self.password)
 


简单主机批量管理工具

标签:div   sof   out   image   insert   简单   space   for   get   

原文地址:https://www.cnblogs.com/xuexiexue/p/12813263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!