-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhcpreserved.py
228 lines (205 loc) · 9.15 KB
/
dhcpreserved.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
228
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2021 Bluecat Networks Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Author B.Shorland - BlueCat Networks 2021
import requests
import argparse
import json
import os
import time
import datetime
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Globals
fixedIPaddress = []
# Login to BAM returns auth token
def login_bam(bamip,username,password):
url = "https://" + bamip + "/Services/REST/v1/login?username=" + username + "&password=" + password + "&"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
token = response.text
token = token.replace('Session Token-> ','')
token = token.replace(' <- for User : apiuser','')
return token
# Logout of BAM, passed IP and auth token
def logout_bam(bamip,token):
url = "http://" + bamip + "/Services/REST/v1/logout?"
payload={}
headers = {}
token=token.replace('"','')
headers['authorization'] = token
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
# Add a UDF Last Reservation to IP4 Addresses in BAM
def addUserDefinedField(bamip,token):
url = "https://" + bamip + "/Services/REST/v1/addUserDefinedField?type=IP4Address"
payload = json.dumps({
"type": "TEXT",
"defaultValue": "",
"validatorProperties": "",
"required": False,
"hideFromSearch": False,
"renderAsRadioButton": False,
"name": "lastreservation",
"displayName": "Last Reservation"
})
headers = {}
token=token.replace('"','')
headers['authorization'] = token
headers['Content-type'] = 'application/json'
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
# Function to return the network ID for a given IP address
def getNetwork(bamip,token,ip_address,configid):
url = "https://" + bamip + "/Services/REST/v1/getIPRangedByIP?containerId=" + configid + "&type=IP4Network&address=" + ip_address + "&"
payload = {}
headers = {}
token=token.replace('"','')
headers['authorization'] = token
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
jsonResponse = response.json()
id = jsonResponse['id']
return id
# Update an entity
def updateEntity(bamip,token, entity):
url = "https://" + bamip + "/Services/REST/v1/update"
payload = {}
headers = {}
token=token.replace('"','')
headers['authorization'] = token
headers['Content-type'] = 'application/json'
response = requests.request("PUT", url, headers=headers, data=json.dumps(entity),verify=False)
return response
# use getEntities to get the ID of the DHCP_RESERVED object with the passed IP
def ProcessDHCPReservedUpdate(bamip,token,networkid,ip_address,update):
url = "https://" + bamip + "/Services/REST/v1/getEntities?parentId=" + str(networkid) + "&type=IP4Address&start=0&count=100000&"
payload = {}
headers = {}
token=token.replace('"','')
headers['authorization'] = token
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
jsonResponse = response.json()
for val in jsonResponse:
props = val['properties']
# Rstrip the last | char from props
props = props.rstrip(props[-1])
# print (props)
Dict = dict((x.strip(), y.strip())
for x, y in (element.split('=')
for element in props.split('|')))
if Dict['state'] == "DHCP_RESERVED" and Dict['address'] == ip_address:
val['properties'] = val['properties'] + "lastreservation=" + str(update) + "|"
resp = updateEntity(bamip,token,val)
return(resp)
# Function to convert hex to IP decimal format
def hex_to_ip_decimal(hex_data):
ipaddr = "%i.%i.%i.%i" % (int(hex_data[0:2],16),int(hex_data[2:4],16),int(hex_data[4:6],16),int(hex_data[6:8],16))
return ipaddr
# Function to read syslog in reverse looking for last DHCPACK for passed IP address
def parsesyslog(ip_address,syslog_year):
last_match = []
with open('/var/log/syslog', 'rt') as f:
data = f.readlines()
for line in reversed(data):
if line.__contains__('DHCPACK on '+str(ip_address)):
last_match.append(line)
break
try:
lastreservation = last_match[0].split()[:3]
lastreservation = ' '.join(lastreservation)
# Lastreservation currently in Jun 7 14:30:25 format
lastreservation = str(syslog_year) + " " + lastreservation
lastreservation = datetime.datetime.strptime(lastreservation, '%Y %b %d %H:%M:%S')
except Exception as e:
lastreservation = 'No Reservation'
return ip_address,lastreservation
def main():
# Parse Commandline Arguments
print('dhcpreserved.py v0.1 - Update Address Manager Last Reserved UDF for DHCP_RESERVED objects')
parser = argparse.ArgumentParser(description='dhcpreserved.py v0.1 - Update Address Manager Last Reserved UDF for DHCP_RESERVED objects')
parser.add_argument("--bam", type=str, help="IP Address of Address Manager")
parser.add_argument("--username", type=str, help="username of API user on Address Manager" )
parser.add_argument("--password", type=str, help="password of API user on Address Manager")
args = parser.parse_args()
if not (args.bam):
parser.error('No BAM IP passed, add --bam [BAM IP]')
exit()
else:
bamip = args.bam
if not (args.username):
parser.error('No BAM username parameter passed, add --username [BAM username]')
exit()
else:
bamuser = args.username
if not (args.password):
parser.error('No BAM parameter passed, add --password [BAM password]')
exit()
else:
bampass = args.password
# Login to Address Manager
mytoken = login_bam(bamip,bamuser,bampass)
# Get the BDDS server.id which contains the server entityID, used to work out configuration for this BDDS
if os.path.isfile('/usr/local/bluecat/server.id'):
pathtoserverid = "/usr/local/bluecat/server.id"
serverid_file = open(pathtoserverid,'r')
serverid = serverid_file.read()
serverid_file.close()
url = "https://172.17.44.90/Services/REST/v1/getParent?entityId=" + serverid + "&"
payload = {}
headers = {}
mytoken=mytoken.replace('"','')
headers['authorization'] = mytoken
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
jsonResponse = response.json()
configid = jsonResponse['id']
else:
print ("BDDS has no Server.id found, new server never under BAM control? exiting ...")
exit()
# Read the fixedIPaddress.dat used by DHCPMON to track DHCP_RESERVED addresses, convert all IPs from HEX to decimal IP address list
with open('/usr/local/bluecat/fixedIPaddress.dat', 'br') as f:
data = f.read(4)
while data:
number = int.from_bytes(data, "big")
ipaddrhex = hex(number)[2:]
result=hex_to_ip_decimal(str(ipaddrhex))
fixedIPaddress.append(result)
data = f.read(4)
print("Number of DHCP Reservations known to DHCPMON:",len(fixedIPaddress))
if len(fixedIPaddress) == 0:
print ("No DHCP Reserved addresses, exiting ....")
exit()
else:
print("List of DHCP Reserved Address[es] known to DHCPMON process:", fixedIPaddress)
# Add the 'Last Reservation' UDF and update the field by reverse scanning of syslog for DHCPAck on IP
addUserDefinedField(bamip, mytoken)
# Work out the year from the syslog created timestamp, we want this to insert an ISO format date later, at default syslog is NOT in ISO format (Pah!)
syslog_created = time.ctime(os.path.getctime("/var/log/syslog"))
syslog_lastmod = time.ctime(os.path.getmtime("/var/log/syslog"))
syslogc = datetime.datetime.strptime(syslog_created, '%a %b %d %H:%M:%S %Y')
syslogm = datetime.datetime.strptime(syslog_lastmod, '%a %b %d %H:%M:%S %Y')
print('Syslog Created: ', syslogc)
print('Syslog Modified: ', syslogm)
syslog_year = syslogc.year
# Loop through IPs in fixedIPaddress sending them for parsing of when the last ACK occured
for ip in fixedIPaddress:
x,y = parsesyslog(ip,syslog_year)
print ("Last DHCP reservation for " + x + " @ " + str(y))
mynetwork = getNetwork(bamip, mytoken,str(ip),str(configid))
id = ProcessDHCPReservedUpdate(bamip, mytoken, mynetwork,str(x),str(y))
print("Update Last Reserved UDF on IP " + str(x) + " " + str(id))
logout_bam(bamip, mytoken)
exit()
if __name__ == "__main__":
main()