Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonTheNiceGuy/9cf38a44365d6a159dd115108c79a0be to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/9cf38a44365d6a159dd115108c79a0be to your computer and use it in GitHub Desktop.

PHPIPAM Bulk Add Subnets.py

This is a simple script to help build up a load of subnets in quick order!

Add your subnets to the list starting at line 76 called "make_subnets", run pip3 install -r requirements.txt and then python3 phpipam_bulk_add_subnets.py

Your result should be like this:

{'code': 201, 'success': True, 'message': 'Subnet created', 'id': '2', 'data': '192.0.2.0/24', 'time': 0.031}

If you have any errors, they will be presented like this:

{'code': 409, 'success': False, 'message': 'Subnet overlaps with 198.51.100.0/24', 'time': 0.009}

Enjoy!

import sys
import requests
from parse_it import ParseIt
from typing import Mapping, Optional, Union
parser = ParseIt()
server = parser.read_configuration_variable('IPAM_SERVER')
api_client = parser.read_configuration_variable('IPAM_API_CLIENT')
api_token = parser.read_configuration_variable('IPAM_API_TOKEN')
if server is None or api_client is None or api_token is None is None:
sys.stderr.write(f"Missing required environment variables. Halting.\n")
exit(1)
def getFromPhpIpam(
server: str,
api_client: str,
api_token: str,
endpoint: str,
data: Optional[
Union[
Mapping[str, str],
None
]
] = None,
secure = True
):
headers = {
'token': api_token,
'Content-Type': 'application/json'
}
if data is None:
pass
else:
headers.update(data)
response = requests.get(f"https://{server}/api/{api_client}/{endpoint}", headers=headers, verify=secure)
return_data = response.json()
if 'data' in return_data:
return return_data['data']
def updatePhpIpam(
server: str,
api_client: str,
api_token: str,
endpoint: str,
id: int,
data: Mapping[str, str],
secure = True
):
headers = {
'token': api_token,
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.patch(f"https://{server}/api/{api_client}/{endpoint}/{id}/", headers=headers, data=data, verify=secure)
return response.json()
def createPhpIpam(
server: str,
api_client: str,
api_token: str,
endpoint: str,
data: Mapping[str, str],
secure = True
):
headers = {
'token': api_token,
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post(f"https://{server}/api/{api_client}/{endpoint}/", headers=headers, data=data, verify=secure)
return response.json()
make_subnets = [
{"subnet": "192.0.2.0", "mask": "24", "description": "First Office Subnet"},
]
if len(make_subnets) == 0:
subnets = getFromPhpIpam(server, api_client, api_token, 'subnets')
print("subnet,parent,section,cidr,description")
for subnet in subnets:
cidr = f"{subnet['subnet']}/{subnet['mask']}"
print(f"'{subnet['id']}','{subnet['masterSubnetId']}','{cidr}','{subnet['description']}'")
else:
for make_subnet in make_subnets:
make_subnet.update( # Limited Changes
{
"sectionId": "1", # Section: Offices
"masterSubnetId": "1", # Subnet: First Office Supernet
'location': '1' # Location: First Office
}
)
make_subnet.update( # Default values
{
'allowRequests': '0',
'showName': '1',
'pingSubnet': '0',
'discoverSubnet': '0',
'resolveDNS': '0',
'DNSrecursive': '0',
'DNSrecords': '0',
'nameserverId': '0',
'scanAgent': '0',
'isFolder': '0',
'isFull': '0',
'isPool': '0',
'threshold': '0'
}
)
print(createPhpIpam(server, api_client, api_token, 'subnets', make_subnet))
requests
parse_it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment