Created
August 26, 2021 12:54
-
-
Save nicosalvadore/2b87fc74ea0b17ec6d642ca405048000 to your computer and use it in GitHub Desktop.
NetBox Python API - Check licenses expiration
This file contains 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 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