#!/usr/bin/python3.8
|
|
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/"
|
|
|
|
headers = {
|
|
'Authorization': API_TOKEN,
|
|
'User-Agent': 'PyScript 0.1',
|
|
'Content-Type': 'application/json',
|
|
'accept': 'application/json'
|
|
}
|
|
|
|
|
|
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:
|
|
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')
|
|
})
|
|
|
|
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):
|
|
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 = (DEVICE_DATA.json()['results'][i]['id'])
|
|
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)
|
|
|