flowtop: Handle return value of gettimeofday()
[netsniff-ng.git] / oui-update.py
blob07add9f52a7072939f921afe4e3f03b6a32267ed
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # update-oui.py -- update netsniff-ng oui.conf from official IEEE OUI list
6 # Copyright (C) 2013 Tobias Klauser <tklauser@distanz.ch>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License version 2 as
10 # published by the Free Software Foundation.
12 import os
13 import sys
14 import re
15 import getopt
16 try:
17 from urllib2 import urlopen # Python 2.x
18 except ImportError:
19 from urllib.request import urlopen # Python 3.x
21 DEFAULT_OUPUT_FILE = "oui.conf"
22 DEFAULT_OUI_URL = "http://standards.ieee.org/develop/regauth/oui/oui.txt"
24 OUI_PATTERN = re.compile(b"^\s*([a-fA-F0-9]{2})-([a-fA-F0-9]{2})-([a-fA-F0-9]{2})\s+\(hex\)\s+(.*)$")
26 def usage():
27 print("""usage: {0} [OPTION...]
28 available options:
29 -f force overwrite of existing file
30 -o set output file (default: {1})
31 -u set URL to fetch OUI list from (default: {2})
32 -h show this help and exit""".format(os.path.basename(sys.argv[0]),
33 DEFAULT_OUPUT_FILE, DEFAULT_OUI_URL))
35 def main():
36 try:
37 opts, args = getopt.getopt(sys.argv[1:], "fo:u:h")
38 except getopt.GetoptError as err:
39 print(str(err))
40 usage()
41 sys.exit(-1)
43 overwrite = False
44 output_file = DEFAULT_OUPUT_FILE
45 oui_url = DEFAULT_OUI_URL
46 for o, a in opts:
47 if o == '-f':
48 overwrite = True
49 elif o == '-o':
50 output_file = a
51 elif o == '-u':
52 oui_url = a
53 elif o == '-h':
54 usage()
55 sys.exit(0)
56 else:
57 assert False, "unhandled option"
59 if not overwrite and os.path.exists(output_file):
60 print("Error: output file {} already exists".format(output_file))
61 sys.exit(-1)
63 print("Updating OUI information in {} from {}... ".format(output_file, oui_url))
65 fh_url = urlopen(oui_url)
67 ouis = []
68 for line in fh_url:
69 m = OUI_PATTERN.match(line)
70 if m:
71 oui = "0x{}{}{}".format(m.group(1), m.group(2), m.group(3))
72 vendor = m.group(4).rstrip()
73 ouis.append((oui, vendor))
75 fh_file = open(output_file, 'w')
76 for oui, vendor in sorted(ouis):
77 fh_file.write("{}, {}\n".format(oui, vendor))
79 fh_url.close()
80 fh_file.close()
82 print("{} OUIs written to {}".format(len(ouis), output_file))
84 if __name__ == '__main__':
85 main()