summaryrefslogtreecommitdiff
path: root/misc/tools/getmac/__main__.py
blob: 5fbe0f768cbe8ff4c877d6321bc1df75b465d73b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function

import argparse
import logging
import sys

from . import getmac


def main():
    parser = argparse.ArgumentParser(
        'getmac', description='Get the MAC address of system network '
                              'interfaces or remote hosts on the LAN')
    parser.add_argument(
        '--version', action='version',
        version='getmac %s' % getmac.__version__)
    parser.add_argument(
        '-v', '--verbose', action='store_true',
        help='Enable output messages')
    parser.add_argument(
        '-d', '--debug', action='count',
        help='Enable debugging output. Add characters to '
             'increase verbosity of output, e.g. \'-dd\'.')
    parser.add_argument(
        '-N', '--no-net', '--no-network-requests',
        action='store_true', dest='NO_NET',
        help='Do not send a UDP packet to refresh the ARP table')

    group = parser.add_mutually_exclusive_group(required=False)
    group.add_argument(
        '-i', '--interface', type=str, default=None,
        help='Name of a network interface on the system')
    group.add_argument(
        '-4', '--ip', type=str, default=None,
        help='IPv4 address of a remote host')
    group.add_argument(
        '-6', '--ip6', type=str, default=None,
        help='IPv6 address of a remote host')
    group.add_argument(
        '-n', '--hostname', type=str, default=None,
        help='Hostname of a remote host')

    args = parser.parse_args()

    if args.debug or args.verbose:
        logging.basicConfig(format='%(levelname)-8s %(message)s',
                            level=logging.DEBUG, stream=sys.stderr)
    if args.debug:
        getmac.DEBUG = args.debug

    mac = getmac.get_mac_address(
        interface=args.interface, ip=args.ip,
        ip6=args.ip6, hostname=args.hostname,
        network_request=not args.NO_NET)

    if mac is not None:
        print(mac)  # noqa: T001
        sys.exit(0)  # Exit success!
    else:
        sys.exit(1)  # Exit with error since it failed to find a MAC


if __name__ == '__main__':
    main()