import paramiko import time import sys ip = "10.1.1.1" username = "Admin" password = "admin123" port = 22 remote_conn_pre=paramiko.SSHClient() remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False) remote_conn = remote_conn_pre.invoke_shell() # time.sleep(2) remote_conn.send('enable ') remote_conn.send(password + ' ') remote_conn.send('terminal pager 0 ') remote_conn.send('show run ') #判断输出是否到最后, 直到所有都输出后才打印结果 result = '' while True: time.sleep(0.5) res = remote_conn.recv(65535).decode('ascii') result += res if res.endswith('# '): sys.stdout.write(result.strip(' ')) break