Category: Programming

Python Loggers

This is a memo about my current standard way of logging in Python, especially when multiple modules are involved. In the main application (app.py): When app.py is started it gets the root logger in line 7. In main() it sets the logging configurations: logging level, formatter and handler. Here the handler is just outputting to […]

Hints for Pynetbox

Note: I don’t yet have extensive experience on NetBox 2.9.x but these have been shown useful in earlier NetBox versions. I’ll update the page later if I find some further comments about the performance in NetBox 2.9.x. Speeding up fetching the devices Fetching lots of devices (hundreds or thousands, depending on the environment) with netbox.dcim.devices.all() […]

Pathlib in Python

I found pathlib standard library in Python just recently. It is a full set of classes, methods, properties and operators that make it very easy to work with files, directories and paths in operating system-independent way. You all do remember that not all filesystems use / (slash) as the path separator…? I had a specific […]

Using Pynetbox

Pynetbox is a Python API client library for NetBox. Here are some basic examples of using pynetbox. In this page: Getting started Creating and changing something Retrieving objects Deleting objects Handling object status Using tags Referring to other objects Here we assume that you already have NetBox running (I have NetBox 2.8.6 in this demo), […]

Subcommands with Argument Parsing in Python

An example how subcommands can be used with argparse module: Sample output: markku@btest:~/temp$ python3 argtest.py usage: argtest.py [-h] [-c CONFIG_FILE] {update-device,add-vlan} … positional arguments: {update-device,add-vlan} update-device update device data add-vlan add a VLAN optional arguments: -h, –help show this help message and exit -c CONFIG_FILE, –config-file CONFIG_FILE specify the configuration file to read (default: argtest.cfg) […]

Webhook Listener for NetBox

Note: There is an improved version in the next post. Following my earlier post about implementing a REST API, here is a simple listener for NetBox webhooks: webhook-listener.py: With the other configurations described in the earlier post (and after configured NetBox to send the webhook POST requests to http://my.server.local/webhook/), these are the outputs: webhook-listener.log: Automatically […]