diff --git a/update_vlans_on_switch.py b/update_vlans_on_switch.py index b268b8a..8436c25 100755 --- a/update_vlans_on_switch.py +++ b/update_vlans_on_switch.py @@ -2,9 +2,10 @@ import requests import json import urllib3 +import ast urllib3.disable_warnings() -API_TOKEN = 'Token 0ee155b764ce14be8a2c2b44b075fcfed9a0f2ec' +API_TOKEN = 'Token 5ea2e025d89ea31674598ceb137c435a2a75c6e4' API_HOST = "https://demo.netbox.dev/" API_APP = "api/dcim/interfaces/" @@ -15,51 +16,26 @@ headers = { 'accept': 'application/json' } -# device_id = (input('Input id of patchpanel:\n')) -device_id = '1' +device_id = (input('Input switch ID:\n')) + vlan_database = { 100: 1, 300: 27, 200: 2 } +vlan_from_switch = [] + +with open('vlanlist.json') as f: + data = ast.literal_eval(f.read()) + for x in data: + vlan_from_switch.append({ + 'id': x.get('id'), + 'untagged_vlan': x.get('untagged_vlan'), + 'tagged_vlans': x.get('tagged_vlans') + }) -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()) +DEVICE_DATA = requests.get(API_HOST + API_APP + '?device_id=' + device_id + '&limit=60', headers=headers, verify=False) +count_of_ports = (DEVICE_DATA.json()['count']) def vid_to_id(vid): @@ -67,7 +43,7 @@ def vid_to_id(vid): for i in vid: vlan_id = vlan_database[i] vid_list.append(vlan_id) - return (vid_list) + return vid_list def untagged_vlans_on_port(portid): @@ -90,26 +66,23 @@ 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) + portid = (DEVICE_DATA.json()['results'][i]['id']) if untagged_vlans_on_port(i) == tagged_vlans_on_port(i): - port_mode = ('access') + port_mode = 'access' dicts.append({'id': portid, - 'mode': port_mode, - 'untagged_vlan': vid_to_id(untagged_vlans_on_port(i))[0]}) + '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') + 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))}) + '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') + port_mode = 'tagged' dicts.append({'id': portid, - 'mode': port_mode, - 'tagged_vlans': vid_to_id(tagged_vlans_on_port(i))}) - - + 'mode': port_mode, + 'tagged_vlans': vid_to_id(tagged_vlans_on_port(i))}) print(json.dumps(dicts, indent=2)) diff --git a/vlanlist.json b/vlanlist.json new file mode 100644 index 0000000..9bc2339 --- /dev/null +++ b/vlanlist.json @@ -0,0 +1,29 @@ +[ + { + "id": 4, + "mode": "", + "untagged_vlan": [200], + "tagged_vlans": [ + 200 + ] + }, + { + "id": 5, + "mode": "", + "untagged_vlan": [100], + "tagged_vlans": [ + 100 + ] + }, + { + "id": 5, + "mode": "", + "untagged_vlan": [], + "tagged_vlans": [ + 100, + 200, + 300 + ] + } +] +