1
votes

I wrote this script to copy an update file to thousands of clients in the field. It works fine with the exception of when a client times out. I'm capturing the exception but it kills my script. Would appreciate some suggestions on how to tweak this script to keep it from dying. Below is the output of the exception and the error killing the script.

('ABCD SAT6', 'Timed Out') [Errno 113] No route to host

Traceback (most recent call last): File "StackUpdateLocoImage.py", line 48, in pool.map(worker, infile) File "/usr/lib64/python2.6/multiprocessing/pool.py", line 148, in map return self.map_async(func, iterable, chunksize).get() File "/usr/lib64/python2.6/multiprocessing/pool.py", line 422, in get raise self._value AttributeError: 'NoneType' object has no attribute 'open_sftp_client'

#!/usr/bin/python
import paramiko, os, string, threading
import socket
import sys
import logging
import multiprocessing

##Python Logging##
LOG_FILENAME = 'updateLocos.out'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG,)
## Paramiko Logging##
paramiko.util.log_to_file("Paramiko_Connections.log")

def worker(line):

    locoid = line.split()
    try:
            if locoid[0] == 'ABCD' :
                    locoip = locoid[4]
            ##Start SFTP
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(locoip, username='user', password='password', timeout=3.0)
    except paramiko.AuthenticationException,e:
            print ('ABCD  ' + locoid[1], 'Auth Error: ',e)
            #exit()
    except paramiko.SSHException,e:
            print "Connection Error: ",e
            #exit()
    except socket.error,e:
            print ('ABCD  ' + locoid[1], 'Timed Out'),e
            #exit()

    filepath = '/usr/local/bin/scripts/update3.2.2.tar.gz'
    localpath = '/root/update3.2.2.tar.gz'
    sftp = ssh.open_sftp()
    sftp.put(filepath, localpath)
    sftp.close()
    ssh.close()

##  set up processing pool
pool = multiprocessing.Pool()

with open('hostslists.txt') as infile:
pool.map(worker, infile)

pool.close()
pool.join()
1

1 Answers

2
votes

You don't catch this error:

AttributeError: 'NoneType' object has no attribute 'open_sftp_client'

The thing is that you have a bunch of except but then you continue the execution like nothing happened, and when it reaches this line:

sftp = ssh.open_sftp()

The variable ssh is None.

So uncomment your exit() or rather change them to return.