Looking up asset tags in Snipe-IT with QR code

In one of my last blog posts I wrote about Creating QR code for wireless networks. I think it would be cool to have QR codes on my devices and link those to my Asset management system. My Asset management system is Snipe-IT and it addresses devices or hardware by an ID in the URL as shown in the screenshot.

Snipe-IT: Asset tag with ID in URL
Snipe-IT: Asset tag with ID in URL

The challenge is now how to “transform” the Asset tag on a device into an ID, which can be called in Snipe-IT’s URL. The API of Snipe-IT helps. At Snipe-ITAPI /hardware/bytag/:asset_tag is the API reference how to get device information by an Asset tag.

Python script to look up ID in Snipe-IT by Asset tag

With this knowledge, I’m writing a small Python script which takes the Asset tag as URL parameter, looks it up via API call in Snipe-IT and then parse out the ID from the JSON result. If Snipe-IT finds a device ID, the script is then forwarding the request to the correct URL with the hardware ID. If there is no result, then the script just forwards to the default page.

    #!/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 = 'https://[HOSTNAME]]'
    token = 'Bearer [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')
Python script to look up ID in Snipe-IT by Asset tag

Generating QR code with Asset tag URL

The QR code must contain the URL of the script with the Asset tag as parameter. To create the QR code, the following command line can be used:

  $qrencode -o assettag.png -l H "https://[CGI-SCRIPT-HOST]/cgi/qrcode-snipeit.py?tag=2020-1024-0001"
Generating QR code with Asset tag URL

The QR code with the Asset tag looks like in the example shown below and can be printed out to a label.

Example QR code with URL to Snipe-IT Asset tag
Example QR code with URL to Snipe-IT Asset tag

Share: