Python paramiko ssh 遠程連接防火牆時報錯-Max try count must be an positive integer

需要使用python遠程連接防火牆,使用paramiko遠程連接並遠程執行shell指令報錯:

調用的代碼如下:

def ssh_normal(remote_ip,ssh_port,user_name,passs_word,cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(remote_ip,username=user_name,password=passs_word,allow_agent=True)
        stdin, stdout, stderr = ssh.exec_command(cmd)
        print stdout.readlines()
        print stderr.readlines()
    except Exception, e:
        print e
    finally:
         ssh.close()

在執行

ssh.exec_command(cmd)

時,報錯:Max try count must be an positive integer。不太清楚報錯的原因,可能是安全策略的原因,改爲以下代碼後可以順利連通並遠程執行命令:

def ssh(remote_ip,username,password,cmds,port=22):
    client = paramiko.SSHClient()
    result=""
    try:
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(remote_ip, port, username, password, timeout=20)
        remote_conn = client.invoke_shell()
        for cmd in cmds:
            remote_conn.send('\n')
            time.sleep(delay)
            remote_conn.send(cmd)
            time.sleep(delay)
            remote_conn.send('\n')
            time.sleep(delay)
            result+=str(remote_conn.recv(buffer_size))
    except Exception, e:
        print e
        result=str(e)
    finally:
        client.close()
    return result

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章