Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telnetlib: A callback for monitoring the telnet session #45035

Closed
knipknap mannequin opened this issue Jun 4, 2007 · 2 comments
Closed

telnetlib: A callback for monitoring the telnet session #45035

knipknap mannequin opened this issue Jun 4, 2007 · 2 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@knipknap
Copy link
Mannequin

knipknap mannequin commented Jun 4, 2007

BPO 1730959
Nosy @jackdied
Files
  • telnetlib.patch: First take
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2009-04-01.00:02:34.469>
    created_at = <Date 2007-06-04.19:41:48.000>
    labels = ['type-feature', 'library']
    title = 'telnetlib: A callback for monitoring the telnet session'
    updated_at = <Date 2009-04-01.00:02:34.375>
    user = 'https://bugs.python.org/knipknap'

    bugs.python.org fields:

    activity = <Date 2009-04-01.00:02:34.375>
    actor = 'jackdied'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-04-01.00:02:34.469>
    closer = 'jackdied'
    components = ['Library (Lib)']
    creation = <Date 2007-06-04.19:41:48.000>
    creator = 'knipknap'
    dependencies = []
    files = ['8027']
    hgrepos = []
    issue_num = 1730959
    keywords = ['patch']
    message_count = 2.0
    messages = ['52707', '84950']
    nosy_count = 2.0
    nosy_names = ['jackdied', 'knipknap']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'test needed'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue1730959'
    versions = ['Python 2.7']

    @knipknap
    Copy link
    Mannequin Author

    knipknap mannequin commented Jun 4, 2007

    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.

    @knipknap knipknap mannequin added the stdlib Python modules in the Lib dir label Jun 4, 2007
    @devdanzin devdanzin mannequin added the type-feature A feature request or enhancement label Mar 30, 2009
    @jackdied
    Copy link
    Contributor

    jackdied commented Apr 1, 2009

    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.

    @jackdied jackdied closed this as completed Apr 1, 2009
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant