189 8069 5689

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"}
    }
}

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

成都创新互联是专业的石家庄网站建设公司,石家庄接单;提供网站设计制作、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行石家庄网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
网站名称:python操作多台服务器-创新互联
网页地址:http://cdxtjz.cn/article/ccdogo.html

其他资讯