简体   繁体   English

telnetlib.write()不复制telnet.interact()

[英]telnetlib.write() doesn't replicate telnet.interact()

I've got this spectrum analyzer from the 90's that has a telnet client on it. 我有90年代的频谱分析仪,上面有一个telnet客户端。 I can telnet in with the Linux telnet CLI, and that works great...controls the machine as expected. 我可以使用Linux telnet CLI进行telnet,并且效果很好...可以按预期控制计算机。

I'd like to automate this thing to do 100's of different measurements in a set-it-and-forget-it style setup, so Python is natural. 我想自动执行此操作,以“设置后忘了”的样式设置进行100次不同的测量,因此Python很自然。

I'm looking at the telnetlib module, and am having some conflicting results...mind you, I'm still in the prototyping phase of this, so I'm using iPython to do this work. 我正在看telnetlib模块,结果有些矛盾……请注意,我仍处于原型开发阶段,所以我正在使用iPython来完成这项工作。

Basically, I can connect and enter an interactive client with telnetlib.interact() , and that works just about as well as the Linux telnet CLI. 基本上,我可以使用telnetlib.interact()连接并输入一个交互式客户端,该客户端的工作原理与Linux telnet CLI差不多。 However, when I copy the exact debug output to the telnetlib.write() method, the instrument doesn't respond. 但是,当我将确切的调试输出复制到telnetlib.write()方法时,仪器没有响应。 Actually, it says something like, undefined header in it's diagnostic output, so the message isn't being transmitted to it correctly. 实际上,它在诊断输出中表示类似undefined header ,因此消息未正确传输给它。

Here's an iPython session output: 这是一个iPython会话输出:

In [2]: import telnetlib

In [3]: HOST = "192.168.0.118"

In [4]: tn = telnetlib.Telnet(HOST, "5024")

In [5]: tn.set
tn.set_debuglevel                   tn.set_option_negotiation_callback  

In [5]: tn.set_debuglevel(2)

In [6]: tn.interact()
Telnet(192.168.0.118,5024): recv b'Welcome to Telnet SCPI Server: agilent\r\nAgilent Te'
Welcome to Telnet SCPI Server: agilent
Agilent TeTelnet(192.168.0.118,5024): recv b'chnologies, N1996A, US45310134, A.02.03\r\n\r\nSCPI>\xff\xfb'
chnologies, N1996A, US45310134, A.02.03

SCPI>Telnet(192.168.0.118,5024): recv b'\x01\xff\xfb\x03'
Telnet(192.168.0.118,5024): IAC WILL 1
Telnet(192.168.0.118,5024): IAC WILL 3
Telnet(192.168.0.118,5024): recv b'\xff\xfc\x01'
Telnet(192.168.0.118,5024): IAC WONT 1
Telnet(192.168.0.118,5024): recv b'\xff\xfc\x03'
Telnet(192.168.0.118,5024): IAC WONT 3
freq:star 110khz 
Telnet(192.168.0.118,5024): send b'freq:star 110khz\n'
Telnet(192.168.0.118,5024): recv b'SCPI>`
SCPI>

however, if I send the same thing with 但是,如果我发送与

tn.write(b'freq:star 110khz\\n') , tn.write(b'freq:star 110khz\\n')

nothing happens...or, if I send 没有任何反应...或者,如果我发送

tn.write(b'freq:star 110khz\\n\\n') , tn.write(b'freq:star 110khz\\n\\n')

nothing happens...no error messages or anything on the instrument. 没有任何反应...没有错误信息或仪器上的任何东西。 It's like nothing even happened. 好像什么都没有发生。

I've seen some issues on stackoverflow that indicate that timing is an issue...I have no idea how to fix that other than by adding the tn.set_debuglevel(2) as a workaround to add delay, but this is probably misunderstanding on my part as well. 我在stackoverflow上看到了一些问题,这些问题表明计时是一个问题...除了添加tn.set_debuglevel(2)作为增加延迟的解决方法外,我不知道如何解决该问题,但这可能是对以下方面的误解我也是。

I do know that this instruments is expecting to see things in character-by-character mode and doesn't say whether is supports line-by-line mode, which is how I assume tn.write(foo) sends stuff. 我确实知道该工具期望以逐字符模式查看内容,而没有说是否支持逐行模式,这就是我假设tn.write(foo)发送东西的方式。

Any ideas where I'm going wrong? 有什么想法我要去哪里吗?

This is a duplicate of this 这是一个重复的这个

Needed to add an \\r to the newline binary portion of the message, so tn.write(b'freq:star 110khz\\r won't work, but tn.write(b'freq:star 110khz\\r\\n will. 需要在邮件的换行二进制部分添加\\r ,因此tn.write(b'freq:star 110khz\\r将不起作用,但tn.write(b'freq:star 110khz\\r\\n将适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM