HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonMinor

Display data based on read configuration file from ssh sessions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fromfilereaddisplaybasedconfigurationdatasshsessions

Problem

I started playing with Python last week. This is a section of code from an application which reads a configuration file and launches ssh sessions and continuously displays data back to the local user.

The whole project is on Github.

def open_connections(sessions):
    """ open_connections(sessions)
    Opens ssh connections and forks them to the background. Passwords are sent
    from the user via STDIN to each process. If correctly identified, the
    process is successfully created and added to the Session object.
    """

    for server in sessions:
        usr = raw_input("Enter username for " + server.address + ": ")
        pwd = getpass.unix_getpass("Enter passphrase for " + server.address + ": ")
        con = paramiko.SSHClient()
        con.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        con.connect(server.address, 
                    username=usr, 
                    password=pwd)
        print "[OK]"
        server.connection = con

def run(sessions):
    """ run(sessions)
    Takes the file descriptors from the open sessions and runs the commands
    at the specified intervals. Display the STDOUT to the current tty.
    """

    while True:
        time.sleep(TIMEOUT)
        os.system("clear")
        for session in sessions:
            print session.command + " @ " + session.address
            print "------------------------------------------------------------"
            stdin, stdout, stderr = session.connection.exec_command(session.command)
            print format_stdout(stdout.readlines())


A couple of my concerns:

  • Am I doing any big Python no-nos?



  • Am I following proper comment/code conventions?



  • Is this the best way to display continuous information to user (screen refreshes)?



This is my first contact with Python so I'm definitely doing something I shouldn't be. The rest of the code for this project is really only 1 file, and what I posted above is the bulk of it. If anyone went to the Github page and wan

Solution

usr = raw_input("Enter username for " + server.address + ": ")
    pwd = getpass.unix_getpass("Enter passphrase for " + server.address + ": ")
    con = paramiko.SSHClient()


I'd avoid using abbreviations. Call them username and password and connection. You don't gain anything using abbreviations and it reduces readability.

for session in sessions:

for server in sessions:


Are they servers or sessions?

I'd also move the contents of the loop into methods on your session objects.

Code Snippets

usr = raw_input("Enter username for " + server.address + ": ")
    pwd = getpass.unix_getpass("Enter passphrase for " + server.address + ": ")
    con = paramiko.SSHClient()
for session in sessions:

for server in sessions:

Context

StackExchange Code Review Q#9195, answer score: 5

Revisions (0)

No revisions yet.