Netbox auto updates
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

118 lines
2.7 KiB

#!/usr/bin/python3.8
import requests
import json
import urllib3
urllib3.disable_warnings()
API_TOKEN = 'Token 0ee155b764ce14be8a2c2b44b075fcfed9a0f2ec'
API_HOST = "https://demo.netbox.dev/"
API_APP = "api/dcim/interfaces/"
headers = {
'Authorization': API_TOKEN,
'User-Agent': 'PyScript 0.1',
'Content-Type': 'application/json',
'accept': 'application/json'
}
# device_id = (input('Input id of patchpanel:\n'))
device_id = '1'
vlan_database = {
100: 1,
300: 27,
200: 2
}
vlan_from_switch = [
{
"id": 1,
"mode": "",
"untagged_vlan": [100],
"tagged_vlans": [
200,
300,
]
},
{
"id": 2,
"mode": "",
"untagged_vlan": [100],
"tagged_vlans": [
100,
]
},
{
"id": 3,
"mode": "",
"untagged_vlan": [],
"tagged_vlans": [
100,
200,
300,
]
},
]
FRONTPORT = requests.get(API_HOST + API_APP + '?device_id=' + device_id + '&limit=60', headers=headers, verify=False)
count_of_ports = (FRONTPORT.json()['count'])
# print(FRONTPORT.json())
def vid_to_id(vid):
vid_list = []
for i in vid:
vlan_id = vlan_database[i]
vid_list.append(vlan_id)
return (vid_list)
def untagged_vlans_on_port(portid):
untagged_vlans_on_port_list = (vlan_from_switch[portid]['untagged_vlan'])
return untagged_vlans_on_port_list
def tagged_vlans_on_port(portid):
tagged_vlans_on_port_list = (vlan_from_switch[portid]['tagged_vlans'])
return tagged_vlans_on_port_list
def interface_id(portid):
interface_id_on_switch = (vlan_from_switch[portid]['id'])
return interface_id_on_switch
dicts = []
ports_on_switch = len(vlan_from_switch)
for i in range(len(vlan_from_switch)):
portid = (FRONTPORT.json()['results'][i]['id'])
# portid = interface_id(i)
if untagged_vlans_on_port(i) == tagged_vlans_on_port(i):
port_mode = ('access')
dicts.append({'id': portid,
'mode': port_mode,
'untagged_vlan': vid_to_id(untagged_vlans_on_port(i))[0]})
if untagged_vlans_on_port(i) != tagged_vlans_on_port(i) and len(untagged_vlans_on_port(i)) > 0:
port_mode = ('tagged')
dicts.append({'id': portid,
'mode': port_mode,
'untagged_vlan': vid_to_id(untagged_vlans_on_port(i))[0],
'tagged_vlans': vid_to_id(tagged_vlans_on_port(i))})
if untagged_vlans_on_port(i) != tagged_vlans_on_port(i) and len(untagged_vlans_on_port(i)) == 0:
port_mode = ('tagged')
dicts.append({'id': portid,
'mode': port_mode,
'tagged_vlans': vid_to_id(tagged_vlans_on_port(i))})
print(json.dumps(dicts, indent=2))
r = requests.patch(API_HOST + API_APP, json=dicts, headers=headers, verify=False)
print(r.json)