python操作多台服务器

发布时间:2020-07-19 13:42:06 作者:期待美好
来源:网络 阅读:295
"""主代码 ""
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"}
    }
}
推荐阅读:
  1. windows 部署多台tomcat
  2. rsync + inotify 实现多台服务器数据同步

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python操作多台服务器 多台

上一篇:单节点3GB单线程967 MB部署半小时内完成,CS领跑分布

下一篇:C#之浅谈虚方法和抽象方法的区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》