This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: telnetlib: A callback for monitoring the telnet session
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: jackdied, knipknap
Priority: normal Keywords: patch

Created on 2007-06-04 19:41 by knipknap, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
telnetlib.patch knipknap, 2007-06-04 19:41 First take
Messages (2)
msg52707 - (view) Author: Samuel Abels (knipknap) Date: 2007-06-04 19:41
When automating a telnet session, you often want to know in realtime what is happening on an active connection. Currently, telnetlib provides the following way to monitor the session:

---------------
import sys, getpass
from telnetlib import Telnet

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")

if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()
---------------

In this case, the last line prints what happened on the session. Often times, you want to watch the session in realtime instead, but telnetlib does not provide a way to do this (AFAIK). I would like to see a callback added into libtelnet, letting you hook into the session such that the following code works:

---------------
import sys, getpass
from telnetlib import Telnet

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

def filter_cb(session, data):
    sys.stdout.write(data)
    return data

tn = Telnet(HOST)
tn.set_data_filter_callback(filter_cb)
tn.read_until("login: ")
tn.write(user + "\n")

if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")
tn.read_until("logout")
---------------

The attached patch (against SVN) implements this behavior.
By taking the return value to replace the original data the callback can also be used to implement filters. This is nice for adapting the behavior of a remote machine, for example for filtering out unwanted characters.

This is my first patch against Python libraries, so apologies if I missed a guideline.
msg84950 - (view) Author: Jack Diederich (jackdied) * (Python committer) Date: 2009-04-01 00:02
class MyTelnet(Telnet):
  def read_until(self, *args)
    txt = Telnet.read_until(self, *args)
    sys.stdout.write(txt)
    return txt

Hope that helps, closing the bug.
History
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 45035
2009-04-01 00:02:34jackdiedsetstatus: open -> closed

nosy: + jackdied
messages: + msg84950

resolution: wont fix
2009-03-30 23:54:32ajaksu2setstage: test needed
type: enhancement
versions: + Python 2.7
2007-06-04 19:41:48knipknapcreate