I am only beginning to code in Python and I still try to figure out how everything works, so if the logic behind it is not right, please correct me.
I am writing a script that queries one device and compares the returned data to another device. The goal is to keep both devices configuration in sync.
Here's the script:
import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
net_objects = ['aaa','availability_group','dns_group','dns_host','group','host','multicast','network','range']
for obj in net_objects:
src_URL = 'https://172.16.2.100:4444/api/objects/network/' + obj + '/'
src_headers = {
'Accept': 'application/json',
'Authorization': 'Basic dG9rZW46U2tCaENIZUlRZnlVeEpBU3dqYUh2c0VHRmZjdWtDTFg='
}
src_net_obj = requests.get(src_URL, headers=src_headers, timeout=15, verify=False)
dst_URL = 'https://172.16.2.101:4444/api/objects/network/' + obj + '/'
dst_headers = {
'Accept': 'application/json',
'X-Restd-Err-Ack': 'all',
'Authorization': 'Basic dG9rZW46dlZ3WnVZZGxpd01IRkxNVXpKVXZtZXhiZGZHSExobnI='
}
dst_net_obj = requests.get(dst_URL, headers=dst_headers, timeout=15, verify=False)
for src_dict_item in src_net_obj.json():
if src_dict_item["name"] in dst_net_obj.json():
update = requests.patch(url, headers=dst_headers, timeout=15, verify=False)
else:
update = requests.put(url, headers=dst_headers, timeout=15, verify=False)
I am not sure about the synthax. Here's what I expect it to do:
- Query the Source device and return JSON formated data
- Query the Destination device and return JSON formated data
- Compare both queries
- If an object in the Source device also exists in the Destination device, update it using a
PATCH
request - If an object in the Source device doesn't exist in the Destination device, create it using a
PUT
request
I am filtering on the name
value returned by the query. If the name exists in both the Source and Destination device, I want it to update the data in the Destination object using the Source object. I would eventually like to compare the actual data in the ocjects in just skip the current object if the data is the same in both Source and Destination, but the will come later.
The returned data from a GET
request on the Source device looks like this:
[{'_locked': '', '_ref': 'REF_DefaultL2TPPool', '_type': 'network/network', 'address': '10.242.3.0', 'address6': 'fd32:5a88:8e98:3::', 'comment': 'Default L2TP VPN Remote Access IP Pool', 'interface': '', 'name': 'VPN Pool (L2TP)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': '', '_ref': 'REF_DefaultPPTPPool', '_type': 'network/network', 'address': '10.242.1.0', 'address6': 'fd32:5a88:8e98:1::', 'comment': 'Default PPTP Remote Access IP Pool', 'interface': '', 'name': 'VPN Pool (PPTP)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': '', '_ref': 'REF_NetNetNetVlan66Publi', '_type': 'network/network', 'address': '192.168.66.0', 'address6': '', 'comment': '', 'interface': '', 'name': 'VLAN 66 Public', 'netmask': 24, 'netmask6': 128, 'resolved': True, 'resolved6': False}, {'_locked': '', '_ref': 'REF_DefaultRWPool', '_type': 'network/network', 'address': '10.242.4.0', 'address6': 'fd32:5a88:8e98:4::', 'comment': 'Default IPsec VPN Remote Access IP Pool', 'interface': '', 'name': 'VPN Pool (IPsec)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': 'user', '_ref': 'REF_NetworkInternet', '_type': 'network/network', 'address': '0.0.0.0', 'address6': '', 'comment': '"Any" network, bound to interfaces with default IPv4 gateway', 'interface': 'REF_IntEthExternaWan', 'name': 'Internet IPv4', 'netmask': 0, 'netmask6': 0, 'resolved': True, 'resolved6': False}, {'_locked': '', '_ref': 'REF_DefaultSSLPool', '_type': 'network/network', 'address': '10.242.2.0', 'address6': 'fd32:5a88:8e98:2::', 'comment': 'Default SSL VPN IP Pool', 'interface': '', 'name': 'VPN Pool (SSL)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': 'user', '_ref': 'REF_NetworkInternet6', '_type': 'network/network', 'address': '0.0.0.0', 'address6': '::', 'comment': '"Any" network, bound to interfaces with default IPv6 gateway', 'interface': '', 'name': 'Internet IPv6', 'netmask': 0, 'netmask6': 0, 'resolved': False, 'resolved6': False}, {'_locked': '', '_ref': 'REF_DefaultCiscoRWPool', '_type': 'network/network', 'address': '10.242.5.0', 'address6': 'fd32:5a88:8e98:5::', 'comment': 'Default IPsec VPN Remote Access IP Pool for Cisco clients', 'interface': '', 'name': 'VPN Pool (Cisco)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': '', '_ref': 'REF_NetNetNetVlan1Site', '_type': 'network/network', 'address': '192.168.0.0', 'address6': '', 'comment': '', 'interface': '', 'name': 'NET - VLAN 1 Site 1', 'netmask': 24, 'netmask6': 128, 'resolved': True, 'resolved6': False}]
Here's the data returned from the GET
request on the Destination device:
[{'_locked': '', '_ref': 'REF_DefaultL2TPPool', '_type': 'network/network', 'address': '10.242.3.0', 'address6': 'fd32:5a88:8e98:3::', 'comment': 'Default L2TP VPN Remote Access IP Pool', 'interface': '', 'name': 'VPN Pool (L2TP)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': '', '_ref': 'REF_DefaultPPTPPool', '_type': 'network/network', 'address': '10.242.1.0', 'address6': 'fd32:5a88:8e98:1::', 'comment': 'Default PPTP Remote Access IP Pool', 'interface': '', 'name': 'VPN Pool (PPTP)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': '', '_ref': 'REF_DefaultRWPool', '_type': 'network/network', 'address': '10.242.4.0', 'address6': 'fd32:5a88:8e98:4::', 'comment': 'Default IPsec VPN Remote Access IP Pool', 'interface': '', 'name': 'VPN Pool (IPsec)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': 'user', '_ref': 'REF_NetworkInternet', '_type': 'network/network', 'address': '0.0.0.0', 'address6': '', 'comment': '"Any" network, bound to interfaces with default IPv4 gateway', 'interface': 'REF_IntEthExternaWan', 'name': 'Internet IPv4', 'netmask': 0, 'netmask6': 0, 'resolved': True, 'resolved6': False}, {'_locked': '', '_ref': 'REF_DefaultSSLPool', '_type': 'network/network', 'address': '10.242.2.0', 'address6': 'fd32:5a88:8e98:2::', 'comment': 'Default SSL VPN IP Pool', 'interface': '', 'name': 'VPN Pool (SSL)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}, {'_locked': 'user', '_ref': 'REF_NetworkInternet6', '_type': 'network/network', 'address': '0.0.0.0', 'address6': '::', 'comment': '"Any" network, bound to interfaces with default IPv6 gateway', 'interface': '', 'name': 'Internet IPv6', 'netmask': 0, 'netmask6': 0, 'resolved': False, 'resolved6': False}, {'_locked': '', '_ref': 'REF_DefaultCiscoRWPool', '_type': 'network/network', 'address': '10.242.5.0', 'address6': 'fd32:5a88:8e98:5::', 'comment': 'Default IPsec VPN Remote Access IP Pool for Cisco clients', 'interface': '', 'name': 'VPN Pool (Cisco)', 'netmask': 24, 'netmask6': 64, 'resolved': True, 'resolved6': True}]
I am trying to compare that returned data from the Source to the data returned from the Destination.
This obviously doesn't work as I expect it to work. I have tried multiple combinations but I can't seem to find the right way to do it.
Any help would be appreciated.