I am looking for a way to resize terminal when using telnetlib. I have achieved a similar effect on SSH with Paramiko’s resize_pty, but I need to support telnet protocol too. Is this even possible (does telnet even have a control stream)?
Note that telnetlib is not a requirement – if there is a better library I would be happy to use it.
UPDATE (more background): I am building a web-based interface for connecting to networking devices. Frontend is built using JS/AJAX, it basically just sends keystrokes to backend and receives screen content from it. Backend is written in Python and takes care of opening a SSH/telnet session to a device, sends keystrokes to it and fetches an output stream, which is then passed through VT100 virtual terminal (pyte). The contents of virtual screen are then sent back to frontend. The problem arises when user wants to resize the terminal screen size in his browser. With SSH I just send resize_pty() through Paramiko and then also resize the pyte’s virtual terminal screen size. But with telnet I was unable to find the appropriate resize function that would tell the device that it should resize its terminal. Is this possible?
Ok, I’ve been able to assemble the following masterpiece:
Now a few words of explanation.
First of all,
telnetlibdoes not support sending commands directly; it simply escapes them. Thus, to send the command we have to use the underlying socket directly. We do that using theget_socket()method of theTelnetobject instance (there).The
NAWScommand assembled here is defined by RFC 1073. Thewidthandheightvariables are regular Python integers which get packed into two 16-bit unsigned integers.Note that this isn’t a perfect solution and I’m not sure if it will actually work for you. Most importantly, during the capabilities negotiation,
telnetlibwill inform the server that itWON'T NAWS, so a particular server may actually ignore the commands.If that’s the case, you’d probably have to use set_option_negotiation_callback(). Sadly, that means you will have to handle all the options which normally
telnetlibdoes for you. And AFAIK it has no conveniences for that.