我编写了一个简单的代码来在Cisco路由器上创建环回接口。
read_all的命令运行正常。以下是我的代码可以有人请检查并告知这里可能有什么问题:
CODE:
**********
import getpass
import telnetlib
HOST = "192.168.0.7"
user = input("Enter your username: ")
password = getpass.getpass()
tn =telnetlib.Telnet(HOST,23,1)
tn.write(b'\n')
tn.read_until(b"Username: ")
tn.write(user.encode("ASCII") + b"\n")
tn.read_until(b"Password: ")
tn.write (password.encode("ASCII") + b"\n")
tn.write(b"enable\n")
tn.read_until(b"Password: ")
tn.write(b"cisco\n")
tn.write(b"conf t\n\n")
tn.read_until(b"(config)#")
tn.write(b"int lo1179\n")
tn.write(b"exit\n")
read=tn.set_debuglevel(10000)
read=tn.read_all()
sd=open("switch", "w")
sd.write(str(read))
输出错误
C:\Users\Rambo>test.py
Enter your username: aady
Password:
Telnet(192.168.0.7,23): recv b'\r\n'
Telnet(192.168.0.7,23): recv b'R1(config)#'
Telnet(192.168.0.7,23): recv b'i'
Telnet(192.168.0.7,23): recv b'nt lo1179\r\n'
Telnet(192.168.0.7,23): recv b'R1(config-if)#'
Telnet(192.168.0.7,23): recv b'exi'
Telnet(192.168.0.7,23): recv b't'
Telnet(192.168.0.7,23): recv b'\r\n'
Telnet(192.168.0.7,23): recv b'R1(config)#'
Traceback (most recent call last):
File "C:\Users\Rambo\test.py", line 20, in <module>
read=tn.read_all()
File "C:\Users\Rambo\AppData\Local\Programs\Python\Python36-32\lib\telnetlib.p
y", line 333, in read_all
self.fill_rawq()
File "C:\Users\Rambo\AppData\Local\Programs\Python\Python36-32\lib\telnetlib.p
y", line 524, in fill_rawq
buf = self.sock.recv(50)
socket.timeout: timed out