ssh 批量执行命令

# python3.5 + paramiko
# pip 是python的包管理工具,在shell里执行如下命令安装paramoko模块
# pip install paramiko
#

import paramiko

def ssh_bat_cmd(ip, port,username,password,command):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # ssh.connect("172.16.2.10", 22, "root", "123123")
    ssh.connect(ip, port, username, password)   # 注意这里的IP 用户名 密码都是字符串
    #stdin, stdout, stderr = ssh.exec_command(command)
    stdin, stdout, stderr = ssh.exec_command(command) #注意这里的command 是字符串

    stdout_info = stdout.readlines()
    err_info = stderr.readlines()
    if err_info:
        print("{} is failed: {}".format(ip,err_info))
    else:
        print("{} is successful: {}".format(ip, stdout_info))
    ssh.close()

IP_dic  = {
    #    "IP":[port,"username","password"],
    "1.1.1.2":[22,"username","password"],
    "1.1.1.3":[22,"username","password"],
    "1.1.1.4":[22,"username","password"],
}

for ip in IP_dic:
    ssh_bat_cmd(ip, IP_dic[ip][0], IP_dic[ip][1], IP_dic[ip][2], """  df -Th   """)
原文地址:https://www.cnblogs.com/resn/p/6647297.html