首页 > 编程语言 > 详细

多线程并行批量管理远程服务器

时间:2019-03-26 21:14:44      阅读:179      评论:0      收藏:0      [点我收藏+]
import paramiko
import sys
import getpass
import threading
import os

def rcmd(host=None, port=22, user=root, passwd=None, command=None):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, username=user, password=passwd, port=port)
    _, stdout, stderr = ssh.exec_command(command)
    out = stdout.read()
    err = stderr.read()
    if out:
        print([%s] OUT:\n%s % (host, out.decode()))
    if err:
        print([%s] ERROR:\n%s % (host, err.decode()))
    ssh.close()

if __name__ == __main__:
    # rcmd(‘192.168.4.6‘, passwd=‘123456‘, command=‘id root; id wangwu‘)
    if len(sys.argv) != 3:
        print(Usage: %s ipfile "command" % sys.argv[0])
        exit(1)
    if not os.path.isfile(sys.argv[1]):
        print(No such file:, sys.argv[1])
        exit(2)

    ipfile = sys.argv[1]
    command = sys.argv[2]
    password = getpass.getpass()
    with open(ipfile) as fobj:
        for line in fobj:
            ip = line.strip()  # 删除行尾的\n
            # rcmd(ip, passwd=password, command=command)
            t = threading.Thread(target=rcmd, args=(ip,), kwargs={passwd: password, command: command})
            t.start()

 

多线程并行批量管理远程服务器

原文:https://www.cnblogs.com/lsgo/p/10603474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!