[英]Nested telnet in python - Telnet within telnet
I am trying to automate a process wherein I would telnet to RouterA and from that RouterA I would again telnet to RouterB. 我正在尝试自动化一个过程,在该过程中我将telnet到RouterA,然后再从该RouterA telnet到RouterB。 I have the code to telnet to RouterA and its working perfectly fine but I am not able to telnet from RouterA to RouterB.
我有将telnet连到RouterA的代码,并且工作正常,但是我无法从RouterA连到RouterB。 The session becomes unresponsive.
会话无响应。 I am doing something wrong.
我做错了。 Can someone please guide me?
有人可以指导我吗?
Below is my code:
import os
import telnetlib
import re
def telNetCall():
host = "15.xx.xx.xx"
user = "admin"
password = "password"
enable_password = "enable_password"
telnet = telnetlib.Telnet(host)
telnet.read_until('Username: ', 3)
telnet.write(user + '\r')
telnet.read_until('Password: ', 3)
telnet.write(password + '\r')
telnet.write('enable' + '\r\n')
telnet.write(enable_password + '\r\n')
telnet.read_until("#")
telnet.write("telnet 20.xx.xx.xx" + '\r\n')
telnet.read_until('Username: ', 3)
telnet.write(user + '\r')
telnet.read_until('Password: ', 3)
telnet.write(password + '\r')
telnet.write('enable' + '\r\n')
telnet.write(enable_password + '\r\n')
telnet.write("show logg"+ "\r\n")
telnet.write('exit' + '\r')
a = telnet.read_all()
f = open(host, 'w')
f.write(str(a))
telNetCall()
Code is working but it is taking very long time to execute. 代码正在运行,但是执行时间很长。 I believe I am doing something which is not allowing the code to run efficiently.
我相信我在做某些不允许代码高效运行的事情。 If there are any issues with the code suggestions are welcome.
如果代码有任何问题,欢迎提出建议。 Any help is greatly appreciated.
任何帮助是极大的赞赏。
I have figured out the error and now its working fine for me. 我已经弄清了错误,现在对我来说可以正常工作。 It hardly takes 5 seconds now.
现在几乎不需要5秒。
You can use another read_xx() functions like read_until()
or read_eager()
. 您可以使用其他read_xx()函数,如
read_until()
或read_eager()
。
See comment of Telnet.read_all()
below: 请参阅下面的
Telnet.read_all()
注释:
Read all data until EOF;
读取所有数据,直到EOF; block until connection closed.
阻塞直到连接关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.