def create_paramiko_obj(host,command):

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, port=22, username="root", password="admin123.")
    try:
        stdin, stdout, stderr = ssh.exec_command(command)
    except Exception as e:
        print("在主机{host}执行命令{command}失败,原因{error},脚本停止!".format(host=host,command=command,error=e))
        return e
    else:
        res = stdout.read()
    finally:
        ssh.close()
    return str(res,encoding="utf-8")

 

posted on 2019-05-17 16:38  bainianminguo  阅读(145)  评论(0编辑  收藏  举报