From efd544a0dce9b11e51c2a46d5b76c288f596616a Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 25 Apr 2022 07:21:30 +0000 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=BD=D1=8B=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D1=8C=20=D1=81=D1=87=D0=B8=D1=82=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D1=8E=D1=82=D1=81=D1=8F=20=D0=B8=D0=B7=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=B0=20vlanlist.json=20=D0=B0=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B2=20=D1=81=D0=B0=D0=BC=D0=BE=D0=BC=20=D1=81?= =?UTF-8?q?=D0=BA=D1=80=D0=B8=D0=BF=D1=82=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update_vlans_on_switch.py | 83 +++++++++++++-------------------------- vlanlist.json | 29 ++++++++++++++ 2 files changed, 57 insertions(+), 55 deletions(-) create mode 100644 vlanlist.json 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 + ] + } +] +