Last active
September 20, 2021 04:52
-
-
Save nubpro/953d75181450424cf217d2561b15eeea to your computer and use it in GitHub Desktop.
SnipeIT Bulk Check-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# change this | |
API_ENDPOINT = "https://<YOUR URL>.com/api" | |
API_TOKEN = "<YOUR API TOKEN>" | |
# export assets list as JSON | |
json_from_snipeit = {"header":[["ID","Company","Asset Tag","Model","Category","Status","Checked Out To","Location"]],"data":[{"ID":39,"Company":"","Asset Tag":" testing","Model":"Portal Generators","Category":"Portable Generators","Status":"Ready to Deploy","Checked Out To":"","Location":""},{"ID":"","Company":"","Asset Tag":"","Model":"","Category":"","Status":"","Checked Out To":"","Location":""}]} | |
assets = json_from_snipeit.get('data') | |
def checkin(asset_id): | |
url = f"{API_ENDPOINT}/v1/hardware/{asset_id}/checkin" | |
headers = { | |
"Accept": "application/json", | |
"Authorization": "Bearer "+API_TOKEN, | |
"Content-Type": "application/json" | |
} | |
response = requests.request("POST", url, headers=headers) | |
return response | |
def getAssetByAssetTag(asset_tag): | |
return next(iter([a for a in assets if a["Asset Tag"].strip().lower() == asset_tag.strip().lower()]), None) | |
def main(): | |
# enter a list of asset tags you want to be checked-in | |
# don't worry it's case-insensitive | |
checkin_asset_tags = ['tag1', 'tag2'] | |
total_count = len(checkin_asset_tags) | |
success_count = 0 | |
already_in_count = 0 | |
for index, asset_tag in enumerate(checkin_asset_tags): | |
try: | |
print('#'+str(index+1)) | |
asset = getAssetByAssetTag(asset_tag) | |
if (not asset): | |
raise ValueError( | |
'Failed to retrieve asset for ' + asset_tag) | |
asset_id = asset.get('ID') | |
print(f"Checking-in asset tag \'{asset_tag}\' (id: {asset_id})") | |
resp = checkin(asset_id) | |
status = resp.json().get('status') | |
messages = resp.json().get('messages') | |
if (resp.status_code != 200): | |
raise PermissionError("Unauthorized action") | |
if (status != 'success'): | |
if (messages == 'That asset is already checked in.'): | |
already_in_count += 1 | |
raise ValueError( | |
f'Failed to check-in due to \'{messages}\'') | |
else: | |
success_count += 1 | |
print('Successfully checked-in.') | |
except Exception as ex: | |
print('[ERR]', ex) | |
finally: | |
print('\r') | |
print('Success: ', success_count) | |
print('Failed: ', total_count-success_count) | |
print('Total: ', total_count) | |
print('Assets already checked-in: ', already_in_count) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment