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 wanted to comment on something else, I'd be happy to respond.