I’m using telnetlib to connect to device and send some commands over
Everything seems to work fine, but when I try to read response after sending command, command text gets back in wierd format with lot of escape characters
To demonstrate
I connect to device
self.terminal = telnetlib.Telnet(device, port)
I send command (and it gets executed as expected)
self.terminal.write(b'configrn')
And then read response
telnetResponse = self.terminal.read_until(b'(Config)#', self.telnetTimeout)
Response I get is my command in some wierd encoding with lot of escape characters and then the rest of response that is normal
b’x1b7cx1b8x1b[1Cx1b7ox1b8x1b[1Cx1b7nx1b8x1b[1Cx1b7fx1b8x1b[1Cx1b7ix1b8x1b[1Cx1b7gx1b8x1b[1Cnrmydevice(Config)#’
Everything I get back from read_until is perfectly normal with no escape characters and strange encoding except commands I sent, even when I send command that returns some kind of response, everything is readable but my command
Tried encoding and decoding result in any way I could think of, but encoding it just produces different kind of junk
mydevice#7c8[1C7o8[1C7n8[1C7f8[1C7i8[1C7g8[1C
I’m using Python 3.11.5 on Ubuntu in WSL
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy
Even tried with older versions of pyhton (3.9) but nothing changed
2
Answers
In absence of better idea I ended up doing my initial idea, regexing all escape characters from response
It produces output as expected, at least in examples I have for now
This can happen becauseof the way the remote device interprets and echoes back the command. Since you know the command you sent, you can strip it from the response string. For example:
After removing the echoed command using the modified code, your
telnetResponse
variable result in:Referring to your comment:
i added re.sub function to remove the echoed command from the response text, taking into account any control characters that might be present.
I hope this helps