python paramiko自动登录网络设备抓取配置信息

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.1.2', port=22, username='cisco', password='cisco')  # 地址是192.168.1.2,用户名/密码都是cisco
client = ssh.invoke_shell()
def run_cmd(cmd, endswith):   # 形参:cmd命令,结束符
    buff = ''
    client.send(cmd)
    while not buff.endswith(endswith):
        resp = str(client.recv(1024), 'utf-8')
        buff += resp
    return buff
res = ''
res += run_cmd('enable\n', 'Password: ')
res += run_cmd('cisco\n', '#')
res += run_cmd('terminal length 0\n', '#')
res += run_cmd('show run\n', '#')
print(res)
ssh.close()

转载于:https://www.cnblogs.com/guxh/p/9831226.html

你可能感兴趣的:(python paramiko自动登录网络设备抓取配置信息)