diff --git a/update_vlans_on_switch.py b/update_vlans_on_switch.py index 8436c25..19e2e34 100755 --- a/update_vlans_on_switch.py +++ b/update_vlans_on_switch.py @@ -3,8 +3,12 @@ import requests import json import urllib3 import ast +from ipaddress import IPv4Interface +import subprocess +import sys urllib3.disable_warnings() + API_TOKEN = 'Token 5ea2e025d89ea31674598ceb137c435a2a75c6e4' API_HOST = "https://demo.netbox.dev/" API_APP = "api/dcim/interfaces/" @@ -16,13 +20,33 @@ headers = { 'accept': 'application/json' } -device_id = (input('Input switch ID:\n')) + +if len(sys.argv) > 1: + device_id = sys.argv[1] +else: + device_id = (input('Input switch ID:\n')) + +DEVICE_IP = IPv4Interface((requests.get(API_HOST + 'api/dcim/devices/' + device_id, headers=headers, + verify=False).json()['primary_ip']['address'])) + +# get vlan info and write to file +cmd = ['./get_vlan.sh', str(DEVICE_IP.ip)] +run = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) +out, err = run.communicate() + +if run.returncode != 0: + raise SystemExit(err) + +with open('vlanlist.json', 'w') as out_file: + out_file.write(out) +# get vlan info and write to file vlan_database = { 100: 1, 300: 27, 200: 2 } + vlan_from_switch = [] with open('vlanlist.json') as f: