帮助请使用telnetlib模块

嗨,大家好, 几天前,我刚刚开始学习Python,并放了一些 我学到的实践. 因此,我想我可能会尝试写一个 简单的程序(根据我见过的示例),这将使我能够 登录到思科路由器,输入配置模式,更改接口 描述,还可以获取运行配置的快照. 到目前为止,我设法登录并更改界面 配置. tn.read_until()函数似乎有效 正如我所期望的那样,我无法跑步 配置以显示任何读取功能. 请建议........我到目前为止的代码如下: 导入GetPass 导入系统 导入telnetlib #这实际上可以登录并更改一个接口描述 路由器 主机=" xxx.xxx.xxx.xxx" #user = raw_input("输入您的远程帐户:") #password = getPass.getPass() 密码=" tacis345int" tn = telnetlib.telnet(主机) #tn.interact() tn.read_until("密码:") tn.write(密码 +" \ n") #if密码: #tn.read_until("密码:") #tn.write(密码 +" \ n") tn.read_until(" port>") #tn.write("显示版本\ n") #tn.write(" \ n \ n") #tn.read_until(" port>") tn.write(" enable \ n") tn.read_until(" Assword:") tn.write(" tacis345int \ n") tn.read_until("端口#") tn.write(" conf t \ n") tn.read_until(" config)#") tn.write(" int ethernet0 \ n") tn.read_until(" config") tn.write(" desc这是由我的脚本\ n写的") tn.write(" show run \ n") 打印tn.read_very_lazy() tn.write("退出\ n") tn.read_until("#") tn.write(" quit \ n") tn.close() 打印tn.read_all() 我从中获得的唯一输出是: -如果)# 节目 我尝试了其他读取选项,但似乎对我没有任何作用. 我显然缺少一些东西,所以如果有人能散发出一些光明 那很好啊.

# 回答1

最简单的解释是,您无法从全局进行"表演" 配置模式... 尝试类似的东西 #EXIT全局配置模式 tn.write('end \ n') 打印tn.read_until('#') #可在24行之后停止停顿 tn.write('term len 0 \ n') tn.read_until('#') #现在显示整个Running-Config tn.write('show run \ n') 打印tn.read_until('#')

标签: python

添加新评论