File: generate-bacnet-vendors.py

package info (click to toggle)
wireshark 4.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 351,244 kB
  • sloc: ansic: 3,101,885; cpp: 129,710; xml: 100,972; python: 56,512; perl: 24,575; sh: 5,874; lex: 4,383; pascal: 4,304; makefile: 165; ruby: 113; objc: 91; tcl: 35
file content (60 lines) | stat: -rwxr-xr-x 1,780 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
'''Update the BACNET vendors list.

generate-bacnet-vendors generates output containing BACNET vendor Identifiers.

Copyright 2023 Jaap Keuter <jaap.keuter@xs4all.nl>
based on work by Anish Bhatt <anish@chelsio.com>

'''

import sys
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup

def exit_msg(msg=None, status=1):
    if msg is not None:
        sys.stderr.write(msg + '\n\n')
    sys.stderr.write(__doc__ + '\n')
    sys.exit(status)

req_headers = { 'User-Agent': 'Wireshark generate-bacnet-vendors' }
try:
    req = urllib.request.Request("https://bacnet.org/assigned-vendor-ids/", headers=req_headers)
    response = urllib.request.urlopen(req)
    lines = response.read().decode()
    response.close()
except urllib.error.HTTPError as err:
    exit_msg("HTTP error fetching {0}: {1}".format(url, err.reason))
except urllib.error.URLError as err:
    exit_msg("URL error fetching {0}: {1}".format(url, err.reason))
except OSError as err:
    exit_msg("OS error fetching {0}".format(url, err.strerror))
except Exception:
    exit_msg("Unexpected error:", sys.exc_info()[0])

soup = BeautifulSoup(lines, "html.parser")
table = soup.find('table')
rows = table.findAll('tr')

entry = "static const value_string\nBACnetVendorIdentifiers [] = {"

for tr in rows:
    cols = tr.findAll('td')
    for index,td in enumerate(cols[0:2]):
        text = ''.join(td.find(string=True))
        if index == 0:
            entry = "    { %4s" % text
        else:
            entry += ", \"%s\" }," % text.rstrip()
    print(entry)

entry = "    { 0, NULL }\n};"
print(entry)