Browse Source

Absolute new version

master
Artur 4 years ago
parent
commit
f22d28c7e0
3 changed files with 58 additions and 131 deletions
  1. +0
    -78
      send_update_temp.pp.py
  2. +58
    -0
      update_labels_on_patchpanel.py
  3. +0
    -53
      update_patchpanel_label.py

+ 0
- 78
send_update_temp.pp.py View File

@ -1,78 +0,0 @@
#!/usr/bin/python3.8
import requests
import json
import urllib3
urllib3.disable_warnings()
url = "https://demo.netbox.dev/api/dcim/front-ports/"
API_HOST = 'https://demo.netbox.dev/api'
TYPE = '/dcim/front-ports/'
headers = {
'Authorization': "Token 8554b98861e47bc6a6fea03e5c4629c3659cacc5",
'User-Agent': 'PyScript 0.1',
'Content-Type': 'application/json',
'accept': 'application/json'
}
data = [
{"id": 625, "label": "dasdas"},
{"id": 626, "label": "d"},
{"id": 627, "label": "sad"},
{"id": 628, "label": "as"},
{"id": 629, "label": "d"},
{"id": 630, "label": "sad"},
{"id": 631, "label": "as"},
{"id": 632, "label": "d"},
{"id": 633, "label": "asd"},
{"id": 634, "label": "sa"},
{"id": 635, "label": "d"},
{"id": 636, "label": "asd"},
{"id": 637, "label": "as"},
{"id": 638, "label": "d"},
{"id": 639, "label": "as"},
{"id": 640, "label": "da"},
{"id": 641, "label": "sd"},
{"id": 642, "label": "as"},
{"id": 643, "label": "d"},
{"id": 644, "label": "as"},
{"id": 645, "label": "da"},
{"id": 646, "label": ""},
{"id": 647, "label": ""},
{"id": 648, "label": ""},
{"id": 649, "label": ""},
{"id": 650, "label": ""},
{"id": 651, "label": ""},
{"id": 652, "label": ""},
{"id": 653, "label": ""},
{"id": 654, "label": ""},
{"id": 655, "label": ""},
{"id": 656, "label": ""},
{"id": 657, "label": ""},
{"id": 658, "label": ""},
{"id": 659, "label": ""},
{"id": 660, "label": ""},
{"id": 661, "label": ""},
{"id": 662, "label": "dasd"},
{"id": 663, "label": "as"},
{"id": 664, "label": "das"},
{"id": 665, "label": "d"},
{"id": 666, "label": "as"},
{"id": 667, "label": "das"},
{"id": 668, "label": "d"},
{"id": 669, "label": "a"},
{"id": 670, "label": "das"},
{"id": 671, "label": "ad"},
{"id": 672, "label": ""},
]
r = requests.patch(API_HOST+TYPE, json=data, headers=headers, verify=False)
print(r.status_code)
print(r.json)
print(type(data))

+ 58
- 0
update_labels_on_patchpanel.py View File

@ -0,0 +1,58 @@
#!/usr/bin/python3.8
import requests
# import json
import urllib3
urllib3.disable_warnings()
API_TOKEN = 'Token 367579298e03d2fd43df86e97b487fe5782d868d'
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}) в патчпанеле')

+ 0
- 53
update_patchpanel_label.py View File

@ -1,53 +0,0 @@
#!/usr/bin/python3.8
import requests
import json
import urllib3
id_or_name=(input('Input id of patchpanel:\n'))
#print(type(id_or_name))
urllib3.disable_warnings()
API_HOST = 'https://demo.netbox.dev/api'
TYPE = '/dcim/front-ports/'
API_FRONTPORT = '/dcim/front-ports/?device_id='+id_or_name
headers = {
'Authorization': "Token 8554b98861e47bc6a6fea03e5c4629c3659cacc5",
'User-Agent': 'PyScript 0.1',
'Content-Type': 'application/json',
'accept': 'application/json'
}
FRONTPORT = requests.get(API_HOST+API_FRONTPORT, headers=headers, verify=False)
ports_label = []
with open('list_pp.csv') as f:
for line in f:
ports_label.append(line.strip())
label_list = []
count_of_ports = (FRONTPORT.json()["count"])
count_in_list = len(ports_label)
if count_of_ports == count_in_list:
print('Generating list')
else:
print('Please check list')
print(f'Ports count: {count_of_ports} and ports in list: {count_in_list}')
data = []
for p in range(count_of_ports):
front_port_id = (FRONTPORT.json()["results"][p]["id"])
front_port_label = (ports_label[p])
list_done = (f'{{"id": {front_port_id}, "label": "{front_port_label}"}},')
print(list_done)

Loading…
Cancel
Save