python操作多台服务器

"""主代码 ""
import settings
import paramiko
import threading

class wang(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 commad(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 show_group():
    for index, key in enumerate(settings.msg_dic):
        print(index +1 , key, len(settings.msg_dic[key]))
        host_ip = input(">>(eg:group1)").strip()
        host_ip_drt = settings.msg_dic[host_ip]
        print(host_ip_drt)
        for i in host_ip_drt:
            print(i, host_ip_drt[i]["IP"])
        return host_ip_drt
def run():
    setsit = show_group()
    thread_list = []
    print(setsit)
    while True:
        cmd  = input(">>(cmd)").strip()
        for i in setsit:
            cc = wang(setsit[i]["IP"], setsit[i]["port"], setsit[i]["username"], setsit[i]["password"], cmd)
            #cc.commad()
            t = threading.Thread(target=cc.commad)
            t.start()
            thread_list.append(t)
            print("-"*50)
        for t in thread_list:
            t.join()
            print("-" * 50)

run()

#settings 模块
msg_dic = {
    "group1":{
        "h2" :{"IP": "192.168.3.200", "username": "root", "password": "sadmin", "port": "22"},
        "h3" :{"IP": "192.168.3.201", "username": "root", "password": "sadmin", "port": "22"}
    }
    "group2":{
        "h2" :{"IP": "192.168.3.203", "username": "root", "password": "sadmin", "port": "22"},
        "h3" :{"IP": "192.168.3.204", "username": "root", "password": "sadmin", "port": "22"}
    }
}

新闻标题:python操作多台服务器
网站路径:http://bzwzjz.com/article/jdescg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 高端网站设计 网站建设方案 品牌网站建设 成都做网站建设公司 成都网站建设公司 上市集团网站建设 成都网站设计 定制网站建设多少钱 成都网站建设 营销型网站建设 定制网站设计 成都网站制作 重庆网站制作 成都营销网站建设 定制网站建设 手机网站建设套餐 泸州网站建设 手机网站制作 定制网站制作 网站建设推广 成都网站制作 成都定制网站建设