Beispiel #1
0
def run(
    args, output=sys.stdout, stdin=sys.stdin, stdin_buffer=sys.stdin.buffer
):
    """Observe an Ethernet interface and print DHCP packets."""
    network_monitor = None
    if args.input_file is None:
        if args.interface is None:
            raise ActionScriptError("Required argument: interface")
        cmd = sudo([get_path("/usr/lib/maas/dhcp-monitor"), args.interface])
        network_monitor = subprocess.Popen(
            cmd,
            stdin=subprocess.DEVNULL,
            stdout=subprocess.PIPE,
            stderr=subprocess.DEVNULL,
        )
        infile = network_monitor.stdout
    else:
        if args.input_file == "-":
            mode = os.fstat(stdin.fileno()).st_mode
            if not stat.S_ISFIFO(mode):
                raise ActionScriptError("Expected stdin to be a pipe.")
            infile = stdin_buffer
        else:
            infile = open(args.input_file, "rb")
    return_code = observe_dhcp_packets(input=infile, out=output)
    if return_code is not None:
        raise SystemExit(return_code)
    if network_monitor is not None:
        return_code = network_monitor.poll()
        if return_code is not None:
            raise SystemExit(return_code)
Beispiel #2
0
def run(args,
        output=sys.stdout,
        stdin=sys.stdin,
        stdin_buffer=sys.stdin.buffer):
    """Observe an Ethernet interface and print beaconing packets."""

    # First, become a progress group leader, so that signals can be directed
    # to this process and its children; see p.u.twisted.terminateProcess.
    os.setpgrp()

    network_monitor = None
    if args.input_file is None:
        if args.interface is None:
            raise ActionScriptError("Required argument: interface")
        cmd = sudo([get_path("/usr/lib/maas/beacon-monitor"), args.interface])
        network_monitor = subprocess.Popen(cmd,
                                           stdin=subprocess.DEVNULL,
                                           stdout=subprocess.PIPE)
        infile = network_monitor.stdout
    else:
        if args.input_file == '-':
            mode = os.fstat(stdin.fileno()).st_mode
            if not stat.S_ISFIFO(mode):
                raise ActionScriptError("Expected stdin to be a pipe.")
            infile = stdin_buffer
        else:
            infile = open(args.input_file, "rb")
    return_code = observe_beaconing_packets(input=infile, out=output)
    if return_code is not None:
        raise SystemExit(return_code)
    if network_monitor is not None:
        return_code = network_monitor.poll()
        if return_code is not None:
            raise SystemExit(return_code)
Beispiel #3
0
def validate_ipv4_cidrs(cidrs):
    """Validates that each item in the specified iterable is an IPv4 CIDR.

    :raise ActionScriptError: if one or more item is not an IPv4 CIDR.
    """
    for cidr in cidrs:
        try:
            IPNetwork(cidr, version=4)
        except AddrFormatError:
            raise ActionScriptError("Not a valid IPv4 CIDR: %s" % cidr)
Beispiel #4
0
def adjust_scanning_parameters(ifname_to_scan: str, cidrs: list,
                               interfaces: dict) -> str:
    """Adjust scan settings `ifname_to_scan` is not an interface name.

    If the first argument was a CIDR instead of an interface, add it to the
    list of CIDRs and return the (possibly modified) `ifname_to_scan`.

    :raise ActionScriptError: If the `ifname_to_scan` (the first positional
        argument specified) is neither an interface name nor a CIDR.
    """
    if ifname_to_scan is not None and ifname_to_scan not in interfaces:
        if "/" in ifname_to_scan:
            cidrs.append(ifname_to_scan)
        else:
            raise ActionScriptError(
                "First argument must be an interface or CIDR: %s" %
                ifname_to_scan)
        ifname_to_scan = None
    return ifname_to_scan
Beispiel #5
0
 def raise_exception():
     raise ActionScriptError("Towel.", returncode=42)