zoukankan      html  css  js  c++  java
  • 多线程并行批量管理远程服务器

    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:
    %s' % (host, out.decode()))
        if err:
            print('[%s] ERROR:
    %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()  # 删除行尾的
    
                # rcmd(ip, passwd=password, command=command)
                t = threading.Thread(target=rcmd, args=(ip,), kwargs={'passwd': password, 'command': command})
                t.start()
  • 相关阅读:
    allocator类
    智能指针shared_ptr
    字面值常量类
    转换构造函数
    委托构造函数
    访问说明符&封装
    const成员函数
    函数指针
    constexper和常量表达式
    函数返回数组指针
  • 原文地址:https://www.cnblogs.com/lsgo/p/10603474.html
Copyright © 2011-2022 走看看