Since pynetbox 5.0 it is possible to set the HTTP request connect timeout in your pynetbox.Api instance. Actually, it is not just a timeout value but you can instantiate a full requests.Session() object to customize the HTTP calls. As a side effect, since the SSL/TLS certificate checking parameter can also be set in the session […]
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 […]
Reading CSV Files With Python
It’s not rare that as a technical worker you get some data in CSV format (comma-separated values), and the data has to be read and interpreted some way. MS Excel is the default tool for some to handle those cases, but I’m one of those that never got used to using Excel in external data […]
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) […]
Set Paste Mode in Vim
When pasting indented data to Vim, set it to paste mode: :set paste To go back to normal mode: :set nopaste
Get Linux Distribution Name and Version with Python
Since Debian 10 Buster there is no minor version number in lsb_release output or /etc/os-release anymore. Bug has been filed, but apparently there is no will to get the full version number back. The dist() and linux_distribution() functions in the platform module in Python have been deprecated in 3.5 and removed in 3.8.0: Also, the […]
Even Better Webhook Listener
I made some improvements to the NetBox webhook listener presented in my earlier post. Here is the new one: Example from the log file: Notable changes: Added the message digest verification (X-Hook-Signature header sent by NetBox) Added content length checking as an example of all the worries you need to take care of when publishing […]