Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@candlerb
candlerb / addresses.py
Last active April 2, 2024 00:07
Netbox report to check for missing Primary IP Addresses
from extras.reports import Report
from dcim.models import Device
from virtualization.models import VirtualMachine
from ipam.constants import *
from ipam.models import IPAddress, Prefix
LOOPBACK_ROLES = [
IPADDRESS_ROLE_LOOPBACK,
IPADDRESS_ROLE_ANYCAST,
IPADDRESS_ROLE_VIP,
@candlerb
candlerb / add_device_type_components.py
Last active February 27, 2024 23:14
Custom script to backfill missing components from device type template
"""
This script adds missing components from the device type to selected device(s)
"""
from dcim.models import Device, ConsolePort, ConsoleServerPort, PowerPort, PowerOutlet, Interface, RearPort, FrontPort, DeviceBay
from extras.scripts import Script, MultiObjectVar
class AddDeviceTypeComponents(Script):
class Meta:
name = "Add Device Type Components"
@candlerb
candlerb / gns3man.py
Last active December 5, 2023 22:20
CGI script to allow manipulation of IOSv/IOSvL2 configurations in GNS3 (reset, export/edit/import, restore from snapshot)
#!/usr/bin/python3
"""
CGI tool to manage configurations of IOSv and CSR1000v devices in GNS3
* Restore configurations from snapshots
* Read and edit the config in NVRAM
"""
GNS3_URL = "http://127.0.0.1:3080"
@candlerb
candlerb / go-project-layout.md
Last active August 2, 2023 17:39
Suggestions for go project layout

If someone asked me the question "what layout should I use for my Go code repository?", I'd start by asking back "what are you building: an executable, or a library?"

Single executable

Stage 1: single source file

Create a directory named however you want your final executable to be called (e.g. "mycommand"), change into that directory, and create the following files:

@candlerb
candlerb / add-device-ports.py
Last active June 24, 2023 14:38
Netbox script to add missing components from Device Type to all instances of Device
#!/opt/netbox/venv/bin/python
import django
import os
import sys
sys.path.append('/opt/netbox/netbox')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netbox.settings')
django.setup()
from dcim.models import Device, ConsolePort, ConsoleServerPort, PowerPort, PowerOutlet, Interface, RearPort, FrontPort, DeviceBay
@candlerb
candlerb / missing-device-ports.py
Last active March 31, 2023 14:33
Netbox report to highlight devices which are missing components that are defined in the Device Type
# Install as reports/missing-device-ports.py
# Run as: python3 manage.py runreport missing-device-ports
from extras.reports import Report
from dcim.models import Device
class MissingDevicePorts(Report):
description = "Find devices which are missing ports that are in the device type template"
def test_add_ports(self):
@candlerb
candlerb / missing_device_type_components.py
Last active January 10, 2023 19:26
Netbox report to identify devices which are missing items from device type
# Identify devices which are missing components from the device type definition
from extras.reports import Report
from dcim.models import Device
class MissingDeviceTypeComponents(Report):
name = "Missing Device Type Components"
description = "Find devices which are missing components that are in the device type template"
def test_add_ports(self):
@candlerb
candlerb / comp4.py
Last active November 24, 2022 11:54
Value chaining, a.k.a. continuations
# Add syntactic sugar so that values flow along a "pipeline":
# A >> B >> C
# becomes C(B(A))
#
# To do this in Python we need to wrap the value in its own class
class Value:
def __init__(self, value):
self.value = value
@candlerb
candlerb / vault.py
Last active February 23, 2022 10:17
python-social-auth plugin for Hashicorp Vault OIDC Provider. See also https://github.com/python-social-auth/social-core/pull/668
"""
Backend for Hashicorp Vault OIDC Identity Provider in Vault 1.9+
https://www.vaultproject.io/docs/secrets/identity/oidc-provider
"""
import base64
from social_core.backends.open_id_connect import OpenIdConnectAuth
from social_core.utils import cache
@candlerb
candlerb / redirector.go
Created February 10, 2022 17:02
Tiny standalone daemon to accept HTTP requests on port 80 and redirect client to HTTPS
package main
import (
"fmt"
"net/http"
"os"
"strings"
)
func redirect(w http.ResponseWriter, req *http.Request) {