Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
from pybatfish.client.session import Session
# Step 1: Set up Pybatfish
bf = Session(host="localhost")
bf.set_network("backup_configs")
# Step 2: Load configurations
SNAPSHOT_DIR = "tests/data/inputs"
bf.init_snapshot(SNAPSHOT_DIR, name="backups", overwrite=True)
@ryanmerolle
ryanmerolle / netaddr_validators.py
Last active October 14, 2023 16:39
netaddr jsonschema validators
#!/usr/bin/env python3
from jsonschema import Draft7Validator, exceptions
import netaddr
# Define the custom validators
def validate_ipv4(validator, ipv4, instance, schema):
try:
ip = netaddr.IPAddress(instance, version=4)
if 'ipv4WithPrefix' in schema and schema['ipv4WithPrefix']:
raise exceptions.ValidationError(f"{instance} should not be a plain IPv4 address when prefix length is expected.")
@ryanmerolle
ryanmerolle / ip_regex.py
Last active November 25, 2021 14:31 — forked from dfee/ip_regex.py
Python IPV4 / IPV6 Regular Expressions (REGEX)
# Constructed with help from
# http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
# Try it on regex101: https://regex101.com/r/yVdrJQ/1
import re
IPV4SEG = r'(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])'
IPV4ADDR = r'(?:(?:' + IPV4SEG + r'\.){3,3}' + IPV4SEG + r')'
IPV6SEG = r'(?:(?:[0-9a-fA-F]){1,4})'
IPV6GROUPS = (