Skip to content

Instantly share code, notes, and snippets.

@nicosalvadore
Created August 26, 2021 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicosalvadore/2b87fc74ea0b17ec6d642ca405048000 to your computer and use it in GitHub Desktop.
Save nicosalvadore/2b87fc74ea0b17ec6d642ca405048000 to your computer and use it in GitHub Desktop.
NetBox Python API - Check licenses expiration
import pynetbox
import requests
import datetime
from dateutil.relativedelta import relativedelta
nb = pynetbox.api(
"https://netbox.domain.net",
token="mysecret",
)
# uncomment below if self-signed cert is used
# session = requests.Session()
# session.verify = False
# nb.http_session = session
# getting next month's date in format YYYY-MM (2021-08)
today = datetime.date.today()
next_month = (today + relativedelta(months=1)).strftime("%Y-%m")
print("Checking licenses expiring during : " + next_month)
# modify this according to what is stored in your devices on netbox.
# we are looking in the "comments" field for a string "licenses-expiration:YYYY-MM-DD"
# for example : licenses-expiration:2021-09-30
prefix = "licenses-expiration:"
# we gather all devices that have licenses expiring next month
devices = nb.dcim.devices.filter(prefix + next_month)
# we print the name and licenses expiring date of each device gathered
for device in devices:
print(device.name + " licenses are expiring on " + device.comments[len(prefix):])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment