[Tutor] Code critique
Bo Morris
crushed26 at gmail.com
Fri Oct 24 14:03:41 CEST 2014
Hello all,
May I please get a little instructional criticism. The code below works. It
logs into 9 different Linux computers, runs a couple commands, and then
transfers a file back to the server. I want to become a better Python
coder; therefore, I was hoping for some ways to make the below code better,
more efficient, or if I am doing something incorrectly, a correct way of
doing it. Thanks
#!/usr/bin/python
import paramiko
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 = ''
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; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01108/3102EHD-01108.png',
'/Downloads/Hourly/3102EHD-01108.png')
sftp.close()
print 'file recieved'
elif '3102EHD-01109' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01109/3102DHD-01109.png',
'/Downloads/Hourly/3102DHD-01109.png')
sftp.close()
print 'file recieved'
elif '3102EHD-MUTV-1082' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01082/3102EHD-01082.png',
'/Downloads/Hourly/3102EHD-01082.png')
sftp.close()
print 'file recieved'
elif '3102DHD-MUTV-1033' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01033/3102DHD-01033.png',
'/Downloads/Hourly/3102DHD-01033.png')
sftp.close()
print 'file recieved'
elif 'Encoder' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01302/3102EHD-01302.png',
'/Downloads/Hourly/3102EHD-01302.png')
sftp.close()
print 'file recieved'
elif '3102DHD-01149' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01149/3102DHD-01149.png',
'/Downloads/Hourly/3102DHD-01149.png')
sftp.close()
print 'file recieved'
elif '3102EHD-01125' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01125/3102EHD-01125.png',
'/Downloads/Hourly/3102EHD-01125.png')
sftp.close()
print 'file recieved'
elif '3102DHD-01144' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01144/3102DHD-01144.png',
'/Downloads/Hourly/3102DHD-01144.png')
sftp.close()
print 'file recieved'
elif '3102EHD-01105' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01105/3102EHD-01105.png',
'/Downloads/Hourly/3102EHD-01105.png')
sftp.close()
print 'file recieved'
con = map(connect, l)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141024/3b0e2ae7/attachment.html>
More information about the Tutor
mailing list