-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_dhcpd_db.py
executable file
·228 lines (200 loc) · 10 KB
/
update_dhcpd_db.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/home/josh/anaconda3/bin/python
# References:
# https://downloads.isc.org/isc/kea/cur/doc/kea-guide.html#dhcp4-std-options
# https://oldkea.isc.org/wiki/HostReservationsHowTo
# https://gitlab.isc.org/isc-projects/kea/wikis/docs/editing-host-reservations
# http://www.lillyrnd.com/index.php/kea-dhcp-server/
import requests, re, csv, yaml, os, sys, time
import mysql.connector
import ipaddress
class kea_db:
def __init__(self):
self.open_database()
self.mycursor = self.mydb.cursor(buffered=True)
# ===========================================================================
@staticmethod
def mac2int(mac):
# ===========================================================================
return int(mac.replace(':', ''),16)
# ===========================================================================
@staticmethod
def int2mac(i):
# ===========================================================================
if type(i) == str:
i = int(i, 16)
s = hex(i).replace('0x', '').zfill(12)
s = re.sub(r'(..)(?=[^$])', r'\1:', s)
return s
# ===========================================================================
@staticmethod
def ip2int(ip):
# ===========================================================================
return int(ipaddress.ip_address(ip))
# ===========================================================================
@staticmethod
def int2ip(i):
# ===========================================================================
return str(ipaddress.ip_address(i))
# ===========================================================================
@staticmethod
def ip2hex(ip):
# ===========================================================================
return hex(int(ipaddress.ip_address(ip))).strip('0x').zfill(8)
# ===========================================================================
@staticmethod
def ip_list2hex(ip_list):
# ===========================================================================
return ''.join(map(kea_db.ip2hex, ip_list))
# ===========================================================================
@staticmethod
def hex_2ip_list(h):
# ===========================================================================
return map(lambda t: str(ipaddress.ip_address(int(t, 16))), h)
# ===========================================================================
def getHostId(self, mac_address):
# ===========================================================================
self.mycursor.execute(f"""select host_id from hosts
where dhcp_identifier =
UNHEX('{mac_address.replace(':', '')}');""")
return self.mycursor.fetchall()
# ===========================================================================
def delete_from_database(self, mac_address):
# ===========================================================================
hosts_id = self.getHostId(mac_address)
for host_id in [t[0] for t in hosts_id]:
self.mycursor.execute(f"delete from dhcp4_options where host_id={host_id};")
self.mycursor.execute(f"delete from dhcp6_options where host_id={host_id};")
self.mycursor.execute(f"delete from hosts where host_id={host_id};")
self.mydb.commit()
# ===========================================================================
def insert_record_to_hosts(self, mac_address, identifier_type, dhcp4_subnet_id, ipv4_address, host_name):
# ===========================================================================
sql_insert = f"""INSERT INTO hosts (dhcp_identifier,
dhcp_identifier_type,
dhcp4_subnet_id,
ipv4_address,
hostname)
VALUES ( UNHEX(REPLACE('{mac_address}', ':', '')),
(SELECT type FROM host_identifier_type
WHERE name='{identifier_type}'),
{dhcp4_subnet_id},
INET_ATON('{ipv4_address}'),
'{host_name}');"""
self.delete_from_database(mac_address)
self.mycursor.execute(sql_insert)
self.mycursor.execute("SELECT LAST_INSERT_ID(); ")
host_id = self.mycursor.fetchone()
return host_id[0]
# ===========================================================================
def set_option(self, host_id, option, value, scope_name='subnet'):
# ===========================================================================
# Option codes: https://www.iana.org/assignments/bootp-dhcp-parameters/bootp-dhcp-parameters.txt
# space: # see https://docs.infoblox.com/display/NAG8/About+IPv4+DHCP+Options
if option == "": return
if option not in ['routers', 'domain-name-servers']:
print(f"Option {option} not currently supported")
sys.exit()
option_codes = {'routers': 3,
'domain-name-servers': 6,
}
dns_options_list = value.replace(' ', '').split(',')
sql_dns = f"""INSERT INTO dhcp4_options (code, value, space, host_id, scope_id)
VALUES ({option_codes[option]},
UNHEX('{self.ip_list2hex(dns_options_list)}'),
'dhcp4',
{host_id},
(SELECT scope_id FROM dhcp_option_scope
WHERE scope_name = '{scope_name}'));"""
self.mycursor.execute(sql_dns)
# ===========================================================================
def print_host_database(self):
# ===========================================================================
sql = """SELECT hosts.host_id,
hex(hosts.dhcp_identifier),
hosts.dhcp_identifier_type,
hosts.dhcp4_subnet_id,
hosts.ipv4_address,
hosts.hostname
FROM hosts """
self.mycursor.execute(sql)
for host_id, mac, _, subnet_id, addr, hostname, in self.mycursor:
print(host_id, self.int2mac(mac), subnet_id, self.int2ip(addr), hostname, sep='\t')
# ===========================================================================
def print_option_database(self):
# ===========================================================================
sql = """SELECT hosts.host_id,
hex(hosts.dhcp_identifier),
hosts.ipv4_address,
hosts.hostname,
dhcp4_options.code,
hex(dhcp4_options.value),
dhcp4_options.space,
dhcp4_options.scope_id
FROM hosts INNER JOIN dhcp4_options
ON hosts.host_id=dhcp4_options.host_id;"""
self.mycursor.execute(sql)
for host_id, mac, addr, hostname, code, value, space, scope_id in self.mycursor:
value = str(value)
l = (len(value) + 7) & ~7 # round length up to a multiple of 8
value = value.zfill(l)
value_list = re.findall(r'(........)', value) if value else []
print(f"{host_id:<7} {self.int2mac(mac):<20} {self.int2ip(addr):<10} {code:>5} {space:>5} {scope_id:>5} {hostname:<15}", *self.hex_2ip_list(value_list))
# ===========================================================================
def print_lease_database(self):
# ===========================================================================
sql = """SELECT address,
hex(hwaddr),
hostname,
valid_lifetime,
unix_timestamp(expire),
state
FROM lease4;"""
self.mycursor.execute(sql)
print(f"{'mac':<20} {'IP':<10} {'lifetime':>10} {'expire':<20} {'state':>5} {'hostname':<15}")
for addr, mac, hostname, lifetime, expire, state in self.mycursor:
print(f'{self.int2mac(mac):<20} {self.int2ip(addr):<10} {lifetime:>10} {time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expire)):<20} {state:>5d} {hostname:<15}')
# ===========================================================================
def get_dns_info(self):
# ===========================================================================
sql = "SELECT hostname, ipv4_address FROM hosts"
self.mycursor.execute(sql)
return [(name, self.int2ip(addr)) for name, addr in self.mycursor]
# ===========================================================================
def open_database(self):
# ===========================================================================
with open(os.path.join(os.path.dirname(__file__), "db.yaml"), 'r') as f:
conf = yaml.load(f, Loader=yaml.BaseLoader)
db_conf = conf['db']
self.mydb = mysql.connector.connect(**db_conf)
# ===========================================================================
def copy_csv_to_db(self, csv_file_name):
# ===========================================================================
f = open(csv_file_name, 'r')
reader = csv.reader(f, delimiter=',')
for row in reader:
host_name, mac_address, ipv4_address, *options_as_list = row
# convert options list [option1, value1, option2, value2, etc.] to a dictionary
t = iter(options_as_list)
options = dict(zip(t, t)) # {option1: value1, option2: value2, etc.}
host_id = self.insert_record_to_hosts(mac_address, 'hw-address', 1024, ipv4_address, host_name)
for option in options:
self.set_option(host_id, option, options[option])
f.close()
self.mydb.commit()
def main():
import sys
if len(sys.argv) != 2:
print("Usage: update_dhcp_db.pb <csv_file_name>")
sys.exit()
if not os.path.isfile(sys.argv[1]):
print(f"Can't find file '{sys.argv[1]}'")
sys.exit()
Kea_DB = kea_db()
csv_file_name = sys.argv[1]
Kea_DB.copy_csv_to_db(csv_file_name)
print("\n\n Hosts Table\n\n")
Kea_DB.print_host_database()
print("\n\n Options Table\n\n")
Kea_DB.print_option_database()
if __name__ == '__main__':
main()