def do_ssh(self, q, lock, name, command):
        empty = False
        q = q or None
        while not empty:
            try:
                ssh = None
                logger = None
                self.logger.debug('Thread: {0}, in Q loop...'.format(name))
                host = None
                try:
                    host = q.get(timeout=self.maxwait)
                except Empty:
                    empty = True
                    break
                start = time.time()
                try:
                    self.logger.debug('Connecting to new host:' + str(host))
                    logger = Eulogger(str(host))
                    ssh = SshConnection(host=host, username=self.username, password=self.password,
                                        keypath=self.keypath, debug_connect=True,
                                        timeout=self.args.timeout, verbose=True, logger=logger)
                    logger.debug('host: {0} running command:{1} '.format(host, command))
                    out = ssh.cmd(str(command), listformat=True, timeout=self.args.timeout,
                                  get_pty=not(self.args.no_pty))
                    logger.debug('Done with host: {0}'.format(host))

                    with lock:
                        self.results[host] = {'status': out.get('status'),
                                              'output': out.get('output'),
                                              'elapsed': int(time.time() - start)}
                except Exception as E:
                    err = "{0}\n{1}".format(get_traceback(), E)
                    with lock:
                        self.results[host] = {'status': -1,
                                              'output': [err],
                                              'elapsed': int(time.time() - start)}
                finally:
                    logger.debug('Closing ssh to host: {0}'.format(host))
                    if ssh:
                        ssh.connection.close()
                        logger.debug('Closed ssh to host: {0}'.format(host))
                    try:
                        if logger:
                            logger.close()
                    except:
                        pass
            except Exception as SE:
                self.logger.error('{0}\nError in do_ssh:{0}'.format(get_traceback(), SE))
            finally:
                if q is not None and not empty:
                    q.task_done()
                self.logger.debug('Finished task in thread:{0}'.format(name))
        self.logger.debug('{0}: Done with thread'.format(name))
Exemple #2
0
    def do_ssh(self, q, lock, name, command):
        empty = False
        q = q or None
        while not empty:
            try:
                ssh = None
                logger = None
                self.logger.debug('Thread: {0}, in Q loop...'.format(name))
                host = None
                try:
                    host = q.get(timeout=self.maxwait)
                except Empty:
                    empty = True
                    break
                start = time.time()
                try:
                    self.logger.debug('Connecting to new host:' + str(host))
                    logger = Eulogger(str(host))
                    ssh = SshConnection(host=host,
                                        username=self.username,
                                        password=self.password,
                                        keypath=self.keypath,
                                        debug_connect=True,
                                        timeout=self.args.timeout,
                                        verbose=True,
                                        logger=logger)
                    logger.debug('host: {0} running command:{1} '.format(
                        host, command))
                    out = ssh.cmd(str(command),
                                  listformat=True,
                                  timeout=self.args.timeout)
                    logger.debug('Done with host: {0}'.format(host))

                    with lock:
                        self.results[host] = {
                            'status': out.get('status'),
                            'output': out.get('output'),
                            'elapsed': int(time.time() - start)
                        }
                except Exception as E:
                    err = "{0}\n{1}".format(get_traceback(), E)
                    with lock:
                        self.results[host] = {
                            'status': -1,
                            'output': [err],
                            'elapsed': int(time.time() - start)
                        }
                finally:
                    logger.debug('Closing ssh to host: {0}'.format(host))
                    if ssh:
                        ssh.connection.close()
                        logger.debug('Closed ssh to host: {0}'.format(host))
                    try:
                        if logger:
                            logger.close()
                    except:
                        pass
            except Exception as SE:
                self.logger.error('{0}\nError in do_ssh:{0}'.format(
                    get_traceback(), SE))
            finally:
                if q is not None and not empty:
                    q.task_done()
                self.logger.debug('Finished task in thread:{0}'.format(name))
        self.logger.debug('{0}: Done with thread'.format(name))