Python网络管理模块Paramiko-代码实例

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\n')
remote_conn.send(password + '\n')
remote_conn.send('terminal pager 0\n')
remote_conn.send('show run\n')
#判断输出是否到最后, 直到所有都输出后才打印结果
result = ''
while True:
    time.sleep(0.5)
    res = remote_conn.recv(65535).decode('ascii')
    result += res
    if res.endswith('# '):
        sys.stdout.write(result.strip('\n'))
        break

猜你喜欢

转载自www.cnblogs.com/konglinqingfeng/p/9585215.html