Esempio n. 1
0
def SSHClient(host_tuple, cmdlist):
    """
    Perform SSH client logic
    """

    host, ip = host_tuple
    # Link-local IPv6 support
    if ip.startswith('fe80'):
        # Try to assume eth0 as the interface as a fallback
        ip = ip+'%'+args.interface if args.interface else ip+"%eth0"

    try:
        # Wait at most CONNECT_TIMEOUT seconds for asyncssh.connect() to return
        with (yield from wait_for(asyncssh.connect(ip, **_SSH_OPTS), CONNECT_TIMEOUT)) as conn:
            conn.usr = conn.get_extra_info("username")
            log.warning('[%s:%s] SSH connection initiated', host, conn.usr)

            for cmd in cmdlist:
                conn.cmd = cmd.strip()
                try:
                    # Initiate command execution
                    session = SSHClientSession
                    # Wait at most SESSION_TIMEOUT seconds for session to complete
                    chan, session = yield from wait_for(
                        conn.create_session(session, conn.cmd), SESSION_TIMEOUT)

                    yield from wait_for(chan.wait_closed(), SESSION_TIMEOUT)

                except AIOTimeout:
                    session.error = "Timeout"

                finally:
                    if session.error:
                        log.critical('[%s:%s] %s (%s)', host, conn.usr, conn.cmd, session.error)
                        log.critical('[%s:%s] Failure detected, breaking...', host, conn.usr)
                        sessionfailures[host] = (cmd, session.error)
                        break # pylint: disable=lost-exception

    except (OSError, asyncssh.Error, AIOTimeout) as e:
        log.error('[%s] SSH connection failed: %s', host, repr(e))
        connectfailures[host] = repr(e)
Esempio n. 2
0
    if args.hostmatch:
        for _ip in _hosts_dict:
            for match in args.hostmatch:
                [_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()