Regarding Telnet library in python

Simon Mullis simon at mullis.co.uk
Wed Aug 13 09:15:21 EDT 2008


Hi there,

This works (but bear in mind I'm about only 30 minutes into my Python
adventure...):

------
    def connect(host):
        tn = telnetlib.Telnet(host)
        return tn

    def login(session,user,password):
        session.write("\n")
        session.read_until("Login: ")
        session.write(user + "\n")
        session.read_until("Password: ")
        session.write(password + "\n")
        session.read_until("#")

    def apply_command(session,command):
        session.write(command + "\n")
        response = str(session.read_until("#"))
        return response

    def cleanup(session):
        session.write("exit\n")
        session.close()

tn = connect(1.1.1.1)

login(tn,user,password)
directory = apply_command(tn,"ls -l")
kernel_release = apply_command(tn,"uname -r\n")
cleanup(tn)

------

You'd probably need to apply a regex to the output of the apply_command
method for it to make sense, and I am confident there is a much, much better
way to do this!

Cheers

SM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080813/b5affbc6/attachment-0001.html>


More information about the Python-list mailing list