【问题标题】:Network Backup Configuration - telnetlib (Python)网络备份配置 - telnetlib (Python)
【发布时间】:2018-01-29 16:13:13
【问题描述】:

我在 python 中有以下脚本,目标是使用 telnet lib 创建 python 脚本并将 Cisco 路由器配置备份到外部文件。但是,每次我运行此脚本时,它都会挂起。任何帮助或建议将不胜感激。

import telnetlib
import getpass
import time
import os


ip_add=raw_input('Please enter IP your would like to back up: ')
os.chdir('/Users/user/Backup_configuration/')

uname = raw_input('Please enter username for {} :'.format(ip_add))
print 'Please enter Password for {} :'.format(ip_add)
passwd = getpass.getpass()


connect = telnetlib.Telnet(ip_add)

connect.read_until('Username: ')
connect.write(uname+'\n')

connect.read_until('Password: ')
connect.write(passwd + '\n')

connect.write('config t\n hostname changed1\n end \n terminal len 0\n wr mem\n' )
time.sleep(3)

connect.write('show run\n')
out = connect.read_all()


filen= open('telnet_file.txt', 'w')
filen.write(out)
filen.close()

【问题讨论】:

    标签: python python-2.7 network-programming telnetlib


    【解决方案1】:

    您需要传递超时值,否则会阻止用户输入。
    尝试以下操作:

    connect = telnetlib.Telnet(ip_add, 23, timeout=1)
    

    telnet lib

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-24
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2018-03-28
      相关资源
      最近更新 更多