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.
 
 

58 lines
1.9 KiB

#!/usr/bin/python3.8
import requests
# import json
import urllib3
urllib3.disable_warnings()
API_TOKEN = 'Token 30498d1f93d710e3d518bc7d3d48a5221b0e48df'
API_HOST = "https://demo.netbox.dev"
API_APP = "api/dcim/front-ports/"
headers = {
'Authorization': API_TOKEN,
'User-Agent': 'PyScript 0.1',
'Content-Type': 'application/json',
'accept': 'application/json'
}
device_id = (input('Input id of patchpanel:\n'))
FRONTPORT = requests.get(API_HOST + API_APP + '?device_id=' + device_id, headers=headers, verify=False)
count_of_ports = (FRONTPORT.json()['count'])
# Read labels from file and create list. After count numbers of them
list_port_labels = []
with open('list_pp.csv') as f:
for line in f:
list_port_labels.append(line.strip())
count_of_labels = len(list_port_labels)
list_port_id = []
def fill_list_port_id(counts):
for p in range(counts):
list_port_id.append(FRONTPORT.json()['results'][p]['id'])
def create_json_list():
json_list = ['id', 'label']
zipped = zip(list_port_id, list_port_labels)
dicts = [dict(zip(json_list, values)) for values in zipped]
print(dicts)
r = requests.patch(API_HOST + API_APP, json=dicts, headers=headers, verify=False)
print(r.json)
print("Count of labels:", count_of_labels, "Count of ports:", count_of_ports)
if count_of_labels == count_of_ports:
print("Списки совпадают. Генерируем")
fill_list_port_id(count_of_labels)
create_json_list()
elif count_of_labels < count_of_ports:
print(f'В списке с названием меньше позиций ({count_of_labels}) чем портов ({count_of_ports}) в патчпанеле')
fill_list_port_id(count_of_labels)
create_json_list()
else:
print(f'В списке с названием больше позиций ({count_of_labels}) чем портов ({count_of_ports}) в патчпанеле')