Python3は、paramikoモジュールとスレッドモジュールを使用して、ホストのバッチ管理を実装し、コマンドを実行します

import paramiko
import sys
from getpass import getpass
import threading

def remote_command(host、pwd、command):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname = host、password = pwd、username = 'root')
    stdin、stdout、stderr = ssh.exec_command(command)
    out = stdout.read()。decode( 'utf8')
    error = stderr.read()。decode( 'utf8')
    if out:
        print( '[%s] OUT:\ n% s '%(host、out))
    エラーの場合:
        print(' [%s] OUT:\ n%s '%(host、error))
    ssh.close()

if __name__ == '
    __main__ ':host_file = sys.argv [1]
    command = sys.argv [2]
    pwd = getpass()
    with open(host_file)as fobj:
        for line in fobj:
            host = line.strip()
            t = threading.Thread(target = remote_command、args =(host、pwd、command))
            t.start()

73件の元の記事を公開 賞賛4 20,000回以上の閲覧

おすすめ

転載: blog.csdn.net/qq_27592485/article/details/102558659