forked from sthierolf/network-automation-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqrcode-snipeit.py
More file actions
50 lines (46 loc) · 1.27 KB
/
Copy pathqrcode-snipeit.py
File metadata and controls
50 lines (46 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.
import requests
import cgi
import cgitb
import json
#
# Snipe-IT HOST and API token
#
snipeit_host = '[SNIPE_IT_HOST]'
token = 'Bearer [SNIPE_IT_AUTH_TOKEN]'
#
# get form data
#
cgitb.enable()
form = cgi.FieldStorage()
#
# handle devices
#
if "tag" in form:
tag = form.getvalue('tag')
url = snipeit_host + '/api/v1/hardware/bytag/'
headers = {'authorization': token}
r = requests.get(url + tag, headers = headers)
#
# if status code is 200, then redirect to hardware
#
if r.status_code == requests.codes.ok:
#
# Snipe-IT must return something like an ID
# If there is no ID, then just list all hardware
#
try:
data = r.json()
device_id = str(data['id'])
print ('Location: ' + snipeit_host + '/hardware/' + device_id + '\n')
except:
print ('Location: ' + snipeit_host + '/hardware/' + '\n')
else:
print ('Location: ' + snipeit_host +'\n')
#
# handle all other errors by just forwarding to the Snipe-IT host
#
else:
print ('Location: ' + snipeit_host +'\n')