python-scripts/kea-mgmt.py

65 lines
1.4 KiB
Python

#!/usr/bin/env python3
import requests
import json
import re
import pprint
url = "http://localhost:8000/"
headers = {"Content-Type": "application/json"}
# Validate for correct IPv4 address syntax
def is_valid_ipv4(ip_address):
ip_regex = re.compile(r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$')
return bool(ip_regex.match(ip_address))
print("Choose action to execute:")
print("1. list all DHCP4 leases")
print("2. resend specific IP address to DNS server")
choice = int(input("Enter choice: "))
if choice == 1:
data = {
"command": "lease4-get-all",
"service": [
"dhcp4"
]
}
elif choice == 2:
while True:
print("Please enter an IPv4 address:")
ip_address = input()
if is_valid_ipv4(ip_address):
break
else:
print("Invalid IPv4 address. Please try again.")
data = {
"command": "lease4-resend-ddns",
"arguments": {
"ip-address": ip_address
},
"service": [
"dhcp4"
]
}
else:
print("Invalid choice.")
exit()
# Get the json data from the server
response = requests.post(url, headers=headers, data=json.dumps(data))
# Make sure the request was successful
response.raise_for_status()
try:
pprint.pprint(response.json())
except json.JSONDecodeError:
print("Error: The server response is not in a valid JSON format.")