Skip to content

Instantly share code, notes, and snippets.

@Midnex
Last active December 4, 2021 17:54
Show Gist options
  • Save Midnex/e46e03e8b0656b76eb3e4ae7d9742aa5 to your computer and use it in GitHub Desktop.
Save Midnex/e46e03e8b0656b76eb3e4ae7d9742aa5 to your computer and use it in GitHub Desktop.
Get the number of Deployableassets in SnipeIT
import json
from collections import Counter
import requests as req
SNIPE_HEADER = {
"Accept": "application/json",
"Authorization": "Bearer ", # after ther space in "Bearer " paste your API key
}
SNIPE_URL = "http://0.0.0.0/api/v1/" # replace IP with domain or ip of snipe server
mod_url = f"{SNIPE_URL}hardware"
querystring = {"offset": "0", "sort": "name", "order": "asc"}
asset_req = req.request("GET", mod_url, headers=SNIPE_HEADER, params=querystring)
assets = json.loads(asset_req.text)["rows"]
data = []
for asset in assets:
if asset["status_label"]["status_meta"] == "deployed": # shows deployed assets.
asset_id = asset["id"]
category = asset["category"]["name"].strip()
data.append(category)
print(Counter(data))
# returns a Counter object of all items in a dictionary.
import json
import requests as req
SNIPE_HEADER = {
"Accept": "application/json",
"Authorization": "Bearer ", # after ther space in "Bearer " paste your API key
}
SNIPE_URL = "http://0.0.0.0/api/v1/" # replace IP with domain or ip of snipe server
mod_url = f"{SNIPE_URL}hardware"
querystring = {"offset": "0", "sort": "id", "order": "asc"}
asset_req = req.request("GET", mod_url, headers=SNIPE_HEADER, params=querystring)
assets = json.loads(asset_req.text)["rows"]
data = []
for asset in assets:
asset_tag = asset["asset_tag"]
category = asset["category"]["name"].strip()
status_name = asset["status_label"]["name"].strip()
status_type = asset["status_label"]["status_type"].strip()
status_meta = asset["status_label"]["status_meta"].strip()
data.append([asset_tag, category, status_name, status_type, status_meta])
with open("assetByCategory.csv", "w") as f:
f.write("asset_tag,category,status_name,status_type,status_meta\n")
for asset in data:
f.write(",".join(asset) + "\n")
if line 21 is changed, possible defaults values [ deployed | deployable | pending | undeployable ]
Checked Out:
asset["status_label"]["status_meta"] == "deployed"
returns:
"status_label": {asssetByCategoryCSV
"id": 2,
"name": "Ready to Deploy",
"status_type": "deployable",
"status_meta": "deployed" <---
},
Checked In:
asset["status_label"]["status_meta"] == "deployable"
returns:
"status_label": {
"id": 2,
"name": "Ready to Deploy",
"status_type": "deployable",
"status_meta": "deployable" <---
},
Pending:
asset["status_label"]["status_meta"] == "pending"
returns:
"status_label": {
"id": 1,
"name": "Pending",
"status_type": "pending",
"status_meta": "pending" <---
},
Un-Deployable:
asset["status_label"]["status_meta"] == "undeployable"
returns:
"status_label": {
"id": 5,
"name": "Defective",
"status_type": "undeployable",
"status_meta": "undeployable" <---
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment