-
Notifications
You must be signed in to change notification settings - Fork 13
/
dhcpclassless
executable file
·55 lines (47 loc) · 1.78 KB
/
dhcpclassless
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
#!/usr/bin/env python3
# dhcpclassless - Generate DHCP option 121 (classless static route)
import argparse
import ipaddress
def n_octets(width):
assert 0 <= width <= 32
return int((width + 7) / 8) # equivalent to int(ceil(width / 8))
parser = argparse.ArgumentParser()
parser.description = "Generates DHCP option 121 (classless static route) values."
parser.add_argument("-d", "--no-default", action="store_true",
help="allow default route to be missing")
parser.add_argument("route", nargs="+",
help="route as 'prefix/len@gateway'")
args = parser.parse_args()
routes = []
has_default = False
for arg in args.route:
network, _, gateway = arg.partition("@")
network = ipaddress.IPv4Network(network or "0.0.0.0/0")
gateway = ipaddress.IPv4Address(gateway or "0.0.0.0")
if network.prefixlen == 0:
has_default = True
routes.append((network.network_address,
network.prefixlen,
gateway))
if not has_default and not args.no_default:
exit("error: Default route was not specified")
routes.sort()
buf = b""
print("Routes:")
for network, prefixlen, gateway in routes:
print(f"\t{network}/{prefixlen} via {gateway}")
buf += bytes([prefixlen])
buf += network.packed[:n_octets(prefixlen)]
buf += gateway.packed
print()
# Windows XP only recognizes option 249; later versions understand 121.
print(f"Option code: 121 (standard), 249 (MS pre-standard)")
print(f"Option data: 0x{buf.hex().upper()}")
print()
print(f"Mikrotik RouterOS:")
print(f"/ip dhcp-server option")
print(f"add name=classless code=121 value=0x{buf.hex().upper()}")
print(f"add name=ms-classless code=249 value=0x{buf.hex().upper()}")
print()
print(f"ISC DHCP (old):")
print(f"option option-121 {':'.join('%x' % c for c in buf)};")