我正在尝试通过 cisco 终端/通信服务器连接到 cisco 节点的控制台连接。为此,我远程登录到特定端口上的 cisco 终端/通信服务器的 IP 地址,让我们说 XXXX - 端口 2068。当我通过 CLI 从我的计算机执行此操作时,它看起来像这样:
[user@computer]$ telnet X.X.X.X 2068
Trying X.X.X.X...
Connected to X.X.X.X (X.X.X.X).
Escape character is '^]'.
Username: <user>
Password:
console-cisco-node>
所以通过我电脑上的 CLI 没问题。但是当我在我的计算机上运行下面的 Python 代码时,它似乎不起作用......
#! /usr/bin/env python
import telnetlib
tn = telnetlib.Telnet("X.X.X.X",2068)
tn.set_debuglevel(8)
data = tn.read_some()
tn.close()
if data == '':
print 'variable data is EMPTY'
else:
print data
print "variable data is FILLED !!!"
当我运行这段代码时,我只能看到这个,看起来'tn.read_some()'只是永远等待,因为没有来自cisco终端/通信服务器的东西?[与 tn.read_all() 相同]
PS。我通过按 CTRL-C 停止了正在运行的代码
[user@computer]$ ./test.py
Telnet(X.X.X.X,2068): recv '\xff\xfb\x01\xff\xfb\x03\xff\xfd\x18\xff\xfd\x1f'
Telnet(X.X.X.X,2068): IAC WILL 1
Telnet(X.X.X.X,2068): IAC WILL 3
Telnet(X.X.X.X,2068): IAC DO 24
Telnet(X.X.X.X,2068): IAC DO 31
Telnet(X.X.X.X,2068): recv '\xff\xfc\x01'
Telnet(X.X.X.X,2068): IAC WONT 1
Telnet(X.X.X.X,2068): recv '\xff\xfc\x03'
Telnet(X.X.X.X,2068): IAC WONT 3
Telnet(X.X.X.X,2068): recv '\xff\xfe\x18'
Telnet(X.X.X.X,2068): IAC DONT 24
Telnet(X.X.X.X,2068): recv '\xff\xfe\x1f'
Telnet(X.X.X.X,2068): IAC DONT 31
Traceback (most recent call last):
File "./test.py", line 7, in ?
data = tn.read_some()
File "/usr/lib64/python2.4/telnetlib.py", line 345, in read_some
self.fill_rawq()
File "/usr/lib64/python2.4/telnetlib.py", line 521, in fill_rawq
buf = self.sock.recv(50)
KeyboardInterrupt
当我将代码中的 'tn.read_some()' 更改为 'tn.read_eager()' 或 'tn.read_very_eager()' 或 'tn.read_lazy()' 或 'tn.read_very_lazy()' 并运行代码它再次向我展示了这一点:
[user@computer]$ ./test.py
variable data is EMPTY
当我将 python 代码更改为不连接到 cisco 节点的控制台连接,而是连接到 cisco 节点的管理连接(正常端口 23 上的另一个 IP 地址 YYYY)时,它工作得很好,我看到了这个输出:
[user@computer]$ ./test1.py
Telnet(Y.Y.Y.Y,23): recv '\xff\xfb\x01\xff\xfb\x03\xff\xfd\x18\xff\xfd\x1f'
Telnet(Y.Y.Y.Y,23): IAC WILL 1
Telnet(Y.Y.Y.Y,23): IAC WILL 3
Telnet(Y.Y.Y.Y,23): IAC DO 24
Telnet(Y.Y.Y.Y,23): IAC DO 31
Telnet(Y.Y.Y.Y,23): recv '\r\n************************************************'
************************************************
variable data is FILLED !!!
所以我认为Python代码还可以。只是 cisco 终端/COMM 服务器 (XXXX) 的反应方式与正常情况不同,我认为 Python telnetlib 很困惑。
有没有人经历过类似的事情?