예제 #1
0
def run(args, stdout=sys.stdout):
    """Sends out beacons, waits for replies, and optionally print debug info.

    :param args: Parsed output of the arguments added in `add_arguments()`.
    :param stdout: Standard output stream to write to.
    """
    # Record the current time so we can figure out how long it took us to
    # do all this scanning.
    logger.configure(DEFAULT_LOG_VERBOSITY, logger.LoggingMode.COMMAND)
    interfaces = get_all_interfaces_definition(annotate_with_monitored=False)
    if args.verbose:
        print("Interface dictionary:\n%s" % pformat(interfaces), file=stdout)
    protocol = do_beaconing(args, interfaces=interfaces)
    if args.verbose:
        print("Transmit queue:\n%s" % pformat(protocol.tx_queue), file=stdout)
        print("Receive queue:\n%s" % pformat(protocol.rx_queue), file=stdout)
예제 #2
0
def run(args, stdout=sys.stdout, stderr=sys.stderr):
    """Scan local networks for on-link hosts.

    :param args: Parsed output of the arguments added in `add_arguments()`.
    :param stdout: Standard output stream to write to.
    :param stderr: Standard error stream to write to.
    """
    # Record the current time so we can figure out how long it took us to
    # do all this scanning.
    ifname_to_scan = args.interface
    cidrs = args.cidr

    # We'll need this data to determine which interfaces we can scan.
    interfaces = get_all_interfaces_definition(annotate_with_monitored=False)

    # We may need to adjust the scanning parameters if the first positional
    # argument was a CIDR and not an interface.
    ifname_to_scan = adjust_scanning_parameters(
        ifname_to_scan, cidrs, interfaces
    )

    # Validate that each CIDR is an IPv4 CIDR. We don't want to spend an
    # eternity trying to scan an IPv6 subnet.
    validate_ipv4_cidrs(cidrs)

    # Get the dictionary of {interfaces: cidrs} to scan.
    to_scan = get_interface_cidrs_to_scan(ifname_to_scan, cidrs, interfaces)

    # Validate that `to_scan` looks sane.
    if ifname_to_scan is not None:
        # This means we're only scanning one interface, so we should warn the
        # user if they requested to scan a CIDR that doesn't exist.
        warn_about_missing_cidrs(ifname_to_scan, cidrs, interfaces, stderr)

    result = scan_networks(args, to_scan, stderr, stdout)
    if result["count"] == 0:
        stderr.write(
            "Requested network(s) not available to scan: %s\n"
            % (", ".join(cidrs) if len(cidrs) > 0 else ifname_to_scan)
        )
        stderr.flush()