Пример #1
0
def main():
    extended = False
    piso = False
    verbose = False
    extended_can_id = False
    sniff_time = 100
    noise_listen_time = 2
    start = None
    end = None
    channel = None
    interface = None
    bitrate = None

    log_handler = logging.StreamHandler(sys.stdout)
    log_handler.setLevel(logging.DEBUG)
    log_runtime.addHandler(log_handler)

    options = getopt.getopt(sys.argv[1:], 'vxCt:n:i:c:b:s:e:hw', [
        'verbose', 'noise_listen_time=', 'sniff_time=', 'interface=', 'piso',
        'channel=', 'bitrate=', 'start=', 'end=', 'help', 'extended',
        'extended_can_id'
    ])

    try:
        for opt, arg in options[0]:
            if opt in ('-v', '--verbose'):
                verbose = True
            elif opt in ('-x', '--extended'):
                extended = True
            elif opt in ('-C', '--piso'):
                piso = True
            elif opt in ('-h', '--help'):
                usage()
                sys.exit(-1)
            elif opt in ('-t', '--sniff_time'):
                sniff_time = int(arg)
            elif opt in ('-n', '--noise_listen_time'):
                noise_listen_time = int(arg)
            elif opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-b', '--bitrate'):
                bitrate = int(arg)
            elif opt in ('-s', '--start'):
                start = int(arg, 16)
            elif opt in ('-e', '--end'):
                end = int(arg, 16)
            elif opt in '--extended_can_id':
                extended_can_id = True
    except getopt.GetoptError as msg:
        usage()
        warning("ERROR: %s" % msg)
        raise SystemExit

    if start is None or \
            end is None or \
            channel is None or \
            (PYTHON_CAN and (bitrate is None or interface is None)):
        usage()
        warning("\nPlease provide all required arguments.\n")
        sys.exit(-1)

    if end >= 2**29 or start >= 2**29:
        warning("Argument 'start' and 'end' must be < " + hex(2**29))
        sys.exit(-1)

    if not extended_can_id and (end >= 0x800 or start >= 0x800):
        warning("Standard can identifiers must be < 0x800.\n"
                "Use --extended_can_id option to scan with "
                "extended CAN identifiers.")
        sys.exit(-1)

    if end < start:
        warning("start must be equal or smaller than end.")
        sys.exit(-1)

    if PYTHON_CAN:
        import can
        try:
            can.rc['interface'] = interface
            can.rc['channel'] = channel
            can.rc['bitrate'] = bitrate
            scan_interface = can.interface.Bus()
            interface_string = "CANSocket(iface=can.interface.Bus(bustype=" \
                               "'%s', channel='%s', bitrate=%d))" % \
                               (interface, channel, bitrate)
        except Exception as e:
            usage()
            warning("\nCheck python-can interface assignment.\n")
            warning(e)
            sys.exit(-1)
    else:
        scan_interface = channel
        interface_string = "\"%s\"" % channel

    try:
        sock = CANSocket(iface=scan_interface)
    except Exception as e:
        usage()
        warning("\nSocket couldn't be created. Check your arguments.\n")
        warning(e)
        sys.exit(-1)

    if verbose:
        print("Start scan (%s - %s)" % (hex(start), hex(end)))

    signal.signal(signal.SIGINT, signal_handler)

    result = ISOTPScan(sock,
                       range(start, end + 1),
                       extended_addressing=extended,
                       noise_listen_time=noise_listen_time,
                       sniff_time=float(sniff_time) / 1000,
                       output_format="code" if piso else "text",
                       can_interface=interface_string,
                       extended_can_id=extended_can_id,
                       verbose=verbose)

    print("Scan: \n%s" % result)
def main():
    extended = False
    piso = False
    verbose = False
    sniff_time = 100
    noise_listen_time = 2
    start = None
    end = None
    channel = None
    interface = None
    bitrate = None

    options = getopt.getopt(sys.argv[1:], 'vxCt:n:i:c:b:s:e:h', [
        'verbose', 'noise_listen_time=', 'sniff_time=', 'interface=', 'piso',
        'channel=', 'bitrate=', 'start=', 'end=', 'help', 'extended'
    ])

    try:
        for opt, arg in options[0]:
            if opt in ('-v', '--verbose'):
                verbose = True
            elif opt in ('-x', '--extended'):
                extended = True
            elif opt in ('-C', '--piso'):
                piso = True
            elif opt in ('-h', '--help'):
                usage()
                sys.exit(-1)
            elif opt in ('-t', '--sniff_time'):
                sniff_time = int(arg)
            elif opt in ('-n', '--noise_listen_time'):
                noise_listen_time = int(arg)
            elif opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-b', '--bitrate'):
                bitrate = int(arg)
            elif opt in ('-s', '--start'):
                start = int(arg, 16)
            elif opt in ('-e', '--end'):
                end = int(arg, 16)
    except getopt.GetoptError as msg:
        usage()
        print("ERROR:", msg, file=sys.stderr)
        raise SystemExit

    if PYTHON_CAN:
        if start is None or \
                end is None or \
                channel is None or \
                bitrate is None or \
                interface is None:
            usage()
            print("\nPlease provide all required arguments.\n",
                  file=sys.stderr)
            sys.exit(-1)
    else:
        if start is None or \
                end is None or \
                channel is None:
            usage()
            print("\nPlease provide all required arguments.\n",
                  file=sys.stderr)
            sys.exit(-1)

    if end >= 0x800:
        print("end must be < 0x800.", file=sys.stderr)
        sys.exit(-1)

    if end < start:
        print("start must be smaller than end.", file=sys.stderr)
        sys.exit(-1)

    if PYTHON_CAN:
        import can
        try:
            can.rc['interface'] = interface
            can.rc['channel'] = channel
            can.rc['bitrate'] = bitrate
            scan_interface = can.interface.Bus()
            interface_string = "CANSocket(iface=can.interface.Bus(bustype=" \
                               "'%s', channel='%s', bitrate=%d))" % \
                               (interface, channel, bitrate)
        except Exception as e:
            usage()
            print("\nCheck python-can interface assignment.\n",
                  file=sys.stderr)
            print(e, file=sys.stderr)
            sys.exit(-1)
    else:
        scan_interface = channel
        interface_string = "\"%s\"" % channel

    try:
        sock = CANSocket(iface=scan_interface)
    except Exception as e:
        usage()
        print("\nSocket couldn't be created. Check your arguments.\n",
              file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(-1)

    if verbose:
        print("Start scan (%s - %s)" % (hex(start), hex(end)))

    signal.signal(signal.SIGINT, signal_handler)

    result = ISOTPScan(sock,
                       range(start, end + 1),
                       extended_addressing=extended,
                       noise_listen_time=noise_listen_time,
                       sniff_time=float(sniff_time) / 1000,
                       output_format="code" if piso else "text",
                       can_interface=interface_string,
                       verbose=verbose)

    print("Scan: \n%s" % result)
def main():
    extended = False
    piso = False
    verbose = False
    extended_can_id = False
    sniff_time = 100
    noise_listen_time = 2
    start = None
    end = None
    channel = None
    interface = None
    python_can_args = None

    options = getopt.getopt(
        sys.argv[1:],
        'vxCt:n:i:c:a:s:e:h:w',
        ['verbose', 'noise_listen_time=', 'sniff_time=', 'interface=', 'piso',
         'channel=', 'python-can_args=', 'start=', 'end=', 'help', 'extended',
         'extended_can_id'])

    try:
        for opt, arg in options[0]:
            if opt in ('-v', '--verbose'):
                verbose = True
            elif opt in ('-x', '--extended'):
                extended = True
            elif opt in ('-C', '--piso'):
                piso = True
            elif opt in ('-h', '--help'):
                usage()
                sys.exit(-1)
            elif opt in ('-t', '--sniff_time'):
                sniff_time = int(arg)
            elif opt in ('-n', '--noise_listen_time'):
                noise_listen_time = int(arg)
            elif opt in ('-i', '--interface'):
                interface = arg
            elif opt in ('-c', '--channel'):
                channel = arg
            elif opt in ('-a', '--python-can_args'):
                python_can_args = arg
            elif opt in ('-s', '--start'):
                start = int(arg, 16)
            elif opt in ('-e', '--end'):
                end = int(arg, 16)
            elif opt in '--extended_can_id':
                extended_can_id = True
    except getopt.GetoptError as msg:
        usage()
        print("ERROR:", msg, file=sys.stderr)
        raise SystemExit

    if start is None or \
            end is None or \
            channel is None or \
            (PYTHON_CAN and interface is None):
        usage()
        print("\nPlease provide all required arguments.\n", file=sys.stderr)
        sys.exit(-1)

    if end >= 2**29 or start >= 2**29:
        print("Argument 'start' and 'end' must be < " + hex(2**29),
              file=sys.stderr)
        sys.exit(-1)

    if not extended_can_id and (end >= 0x800 or start >= 0x800):
        print("Standard can identifiers must be < 0x800.\n"
              "Use --extended_can_id option to scan with "
              "extended CAN identifiers.",
              file=sys.stderr)
        sys.exit(-1)

    if end < start:
        print("start must be equal or smaller than end.", file=sys.stderr)
        sys.exit(-1)

    sock = None

    try:
        if PYTHON_CAN:
            if python_can_args:
                interface_string = "CANSocket(bustype=" \
                                   "'%s', channel='%s', %s)" % \
                                   (interface, channel, python_can_args)
                arg_dict = dict((k, literal_eval(v)) for k, v in
                                (pair.split('=') for pair in
                                 re.split(', | |,', python_can_args)))
                sock = CANSocket(bustype=interface, channel=channel,
                                 **arg_dict)
            else:
                interface_string = "CANSocket(bustype=" \
                                   "'%s', channel='%s')" % \
                                   (interface, channel)
                sock = CANSocket(bustype=interface, channel=channel)
        else:
            sock = CANSocket(channel=channel)
            interface_string = "\"%s\"" % channel

        if verbose:
            print("Start scan (%s - %s)" % (hex(start), hex(end)))

        signal.signal(signal.SIGINT, signal_handler)

        result = ISOTPScan(sock,
                           range(start, end + 1),
                           extended_addressing=extended,
                           noise_listen_time=noise_listen_time,
                           sniff_time=float(sniff_time) / 1000,
                           output_format="code" if piso else "text",
                           can_interface=interface_string,
                           extended_can_id=extended_can_id,
                           verbose=verbose)

        print("Scan: \n%s" % result)

    except Exception as e:
        usage()
        print("\nSocket couldn't be created. Check your arguments.\n",
              file=sys.stderr)
        print(e, file=sys.stderr)
        sys.exit(-1)

    finally:
        if sock is not None:
            sock.close()