Bo Morris wrote: > 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(?) >
As hinted in my previous post I don't think you need a dict here. Once you have the hostname you can build the filename from it with hostname + ".png". Here's what I had in mind, unfortunately totally untested: #!/usr/bin/python import os import paramiko ip_list = [ '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' 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') hostname = stdout.read().strip() filename = hostname + ".png" png_source = os.path.join("/Downloads/Hourly", filename) png_dest = os.path.join("/Downloads/Hourly", hostname, filename) s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh') sftp = s.open_sftp() sftp.get(png_dest, png_source) sftp.close() print "file", png_dest, "received" for ip in ip_list: connect(ip) _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor