|
|
#!/usr/bin/python3.8
|
|
|
import requests
|
|
|
# import json
|
|
|
import urllib3
|
|
|
|
|
|
urllib3.disable_warnings()
|
|
|
API_TOKEN = 'Token ba70a20837155752895ab1860d366812a711e6ea'
|
|
|
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'
|
|
|
}
|
|
|
|
|
|
device_id = (input('Input id of switch:\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}) в патчпанеле')
|