Telnet and running commands on remote systems

Doug Farrell writeson at earthlink.net
Thu Jan 10 05:23:24 CET 2002


Hi all,

I'm working on a program to do release work on our servers, which
means moving files around on various machines and running commands on
those servers (tar, mv, etc.). How could I do this in Python? Should I
use telnetlib or popen or something else? I'm trying to use telnetlib
right now and not having much luck sending commands to the remote
machine and looking at the returned results. I want to have a Python program
send a list of commands to the remote machine and then look at the
results of those commands using string comparisons. I'd like to use the
split() method of string
to split the response up into a list and then return this list to the
caller. The class I've got looks
something like this:

import telnetlib

class Telnet:
    def __init__(self, hostname, username, password):
        """this method connects the session to the remote machine"""
        self.__tn = telnetlib.Telnet(hostname)
        #etc

    def cmd(self, command):
        response = []
        self.__tn.write(command)
        rsp = tn.read_very_eager()
        response = rsp.split("\n")  # split lines up
        return response

Any suggestions or pointers where I might look for examples of Python
telnetlib code would be greatly appreciated.

Thanks,
Doug Farrell






More information about the Python-list mailing list