[Tutor] Code critique
Bo Morris
crushed26 at gmail.com
Sat Oct 25 03:26:47 CEST 2014
Thank you all for the helpful criticism. I wish I was able to catch on to
what you are suggesting more quickly.
Based on your recommendations, I have come up with the following so far,
however I just dont see it as easily as I did while using the if/elif
statements.
This is what I have so far. I can not figure out how to iterate through the
dictionary inserting each value where "png_file" should be and execute the
code for each ip address in the list. I was able to do it using the if/elif
statements, but I am afraid I am lost trying to do it another way.
ipList = ['ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8',
'ip-9']
host = {'3102EHD-01108':'3102EHD-01108.png',
'3102EHD-01109':'3102DHD-01109.png',
'3102EHD-MUTV-1082':'3102EHD-01082.png',
'3102DHD-01033':'3102DHD-MUTV-1033.png',
'Encoder':'3102EHD-01302.png',
'3102DHD-01149':'3102DHD-01149.png',
'3102EHD-01125':'3102EHD-01125.png',
'3102DHD-01144':'3102DHD-01144.png',
'3102EHD-01105':'3102EHD-01105.png'}
# iterate through the dictionary inserting the png file
def get_png_file(?):
process = get_png_file.get(hostname, png_file)
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/'png_file,'/Downloads/Hourly/'png_file)
sftp.close()
print 'file recieved'
user = 'user'
passwd = 'password'
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# iterate through the list and do the below for each
for ip in ipList:
s.connect(ip,22,user,passwd,timeout=4)
####since I have all the hostnames in the dic, and I am removing
####the if/elif statments, do I need the below command 'echo $HOSTNAME'?
stdin, stdout, stderr = s.exec_command('echo $HOSTNAME')
out = stdout.read()
get_png_file(?)
**original code below**
#############connect to each machine and retrieve the image#############
l = ['ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8', 'ip-9']
def connect(ip):
user = 'user'
passwd = 'password'
command = 'echo $HOSTNAME'
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(ip,22,user,passwd,timeout=4)
stdin, stdout, stderr = s.exec_command('echo $HOSTNAME')
out = stdout.read()
if '3102EHD-Lanka-1108' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01108/3102EHD-01108.png',
'/Downloads/Hourly/3102EHD-01108.png')
sftp.close()
elif '3102EHD-01109' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01109/3102DHD-01109.png',
'/Downloads/Hourly/3102DHD-01109.png')
sftp.close()
elif '3102EHD-MUTV-1082' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01082/3102EHD-01082.png',
'/Downloads/Hourly/3102EHD-01082.png')
sftp.close()
elif '3102DHD-MUTV-1033' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01033/3102DHD-01033.png',
'/Downloads/Hourly/3102DHD-01033.png')
sftp.close()
elif 'Encoder' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01302/3102EHD-01302.png',
'/Downloads/Hourly/3102EHD-01302.png')
sftp.close()
elif '3102DHD-01149' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01149/3102DHD-01149.png',
'/Downloads/Hourly/3102DHD-01149.png')
sftp.close()
elif '3102EHD-01125' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01125/3102EHD-01125.png',
'/Downloads/Hourly/3102EHD-01125.png')
sftp.close()
elif '3102DHD-01144' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01144/3102DHD-01144.png',
'/Downloads/Hourly/3102DHD-01144.png')
sftp.close()
elif '3102EHD-01105' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01105/3102EHD-01105.png',
'/Downloads/Hourly/3102EHD-01105.png')
sftp.close()
con = map(connect, l)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141024/8e4cad86/attachment.html>
More information about the Tutor
mailing list