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.
17 from urllib2
import urlopen
# Python 2.x
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+(.*)$")
27 print("""usage: {0} [OPTION...]
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
))
37 opts
, args
= getopt
.getopt(sys
.argv
[1:], "fo:u:h")
38 except getopt
.GetoptError
as err
:
44 output_file
= DEFAULT_OUPUT_FILE
45 oui_url
= DEFAULT_OUI_URL
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
))
63 print("Updating OUI information in {} from {}... ".format(output_file
, oui_url
))
65 fh_url
= urlopen(oui_url
)
69 m
= OUI_PATTERN
.match(line
)
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
))
82 print("{} OUIs written to {}".format(len(ouis
), output_file
))
84 if __name__
== '__main__':