paramiko 遭遇socket.error: Socket is closed 错误的解决办法
似乎是connection自己断了解决的办法是在创建conn的时候添加下面这句
conn.keep_this = conn_session
完整代码
def create_a_conn(ip_addr, port, username, password):
'''
creat a conn to router using paramiko.SSHClient()
'''
conn_session = paramiko.SSHClient()
conn_session.load_system_host_keys()
conn_session.connect(ip_addr, port, username, password, look_for_keys = False, allow_agent = False)
conn = conn_session.invoke_shell()# to keep the session go on
conn.keep_this = conn_session
time.sleep(1)
conn.send("terminal length 0\n")
time.sleep(1)
if conn.recv_ready():
conn.recv(65535)
return conn