Exemple #1
0
def parse_hosts(path):
    """
    Read lines from path and parse entries with split()
    """
    with open(path) as hostsfile:
        hosts = hostsfile.readlines()

    # Parse out comments and blank lines, split on whitespace
    hosts = [line.split() for line in hosts if not line.startswith('#') and line.strip()]
    log.debug('Parsed %d entries from %s', len(hosts), path)

    # Duplicate logic
    dups = 0
    for line in hosts:
        hosts.remove(line)
        for dup_check in hosts:
            if dup_check[1] == line[1]:
                dup = True
                break
        if not dup:
            hosts.append(line)
        else:
            dups += 1
        dup = False
    log.debug('Ignored %d duplicate hosts', dups)

    # Return dictionary of ip: [hostnames,..]
    return {line[0]: line[1:] for line in hosts}
Exemple #2
0
                [_inc_hosts.add((host, _ip)) for host in _hosts_dict[_ip] if match in host] # pylint: disable=expression-not-assigned

    # Host exclusion logic
    _exc_hosts = set()
    if args.hostexclude:
        for exclude in args.hostexclude:
            [_exc_hosts.add((host, ip)) for (host, ip) in _inc_hosts if exclude in host] # pylint: disable=expression-not-assigned

    # Bitwise XOR on inclusion/exclusion set() objects
    _hosts = _inc_hosts ^ _exc_hosts

    if not _hosts:
        log.critical('No hosts matched')
        log_queue.stop()
        sys.exit(1)
    log.debug('Matched %d hosts like %s, unlike %s',
              len(_hosts), args.hostmatch, args.hostexclude or "''")

    # Place matched hosts into queue
    #for _ in range(200):
    [_host_queue.put_nowait(sorted_host) for sorted_host in sorted(_hosts)] # pylint: disable=expression-not-assigned
    _host_count = _host_queue.qsize()

    try:
        # Open output script for writing
        output = open(args.output, 'w')

        _start = loop.time()
        # Begin asynchronous loop execution
        loop.run_until_complete(SSHManager(_host_queue, args.cmdlist))

    finally: