Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View tyler-8's full-sized avatar

Tyler Bigler tyler-8

View GitHub Profile
@tyler-8
tyler-8 / mp_netmiko.py
Last active January 16, 2017 20:58
Multiprocessing with Netmiko for Plaid Speed
from __future__ import print_function
import logging
import time
from datetime import datetime
from multiprocessing import Pool, cpu_count, current_process
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException, NetMikoAuthenticationException
@tyler-8
tyler-8 / ping.py
Last active January 17, 2017 18:57
Simple function for pinging an IP/hostname (Linux/Mac Only)
import re
import subprocess
def ping(host):
""" Ping the address/hostname and return True if packet loss is less than
60%. All other results return False or print and error."""
exp = re.compile(r"\s(\d{1,3})\%\s")
try:
test = subprocess.Popen(["ping", "-c 5", "-W 2", host],
Traceback (most recent call last):
File "/venv/3.6.4/envs/fmt/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/venv/3.6.4/envs/fmt/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/venv/3.6.4/envs/fmt/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/venv/3.6.4/envs/fmt/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/venv/3.6.4/envs/fmt/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
@tyler-8
tyler-8 / portlist.py
Created April 4, 2018 18:36
Parse the PortList output from SNMP OIDs like 'ieee8021QBridgeVlanCurrentEgressPorts'
def parsePortList(portlist):
"""
Returns indexes of ports where
VLAN is present.
"""
port_status = []
present_ports = []
for values in portlist:
for i, bit in enumerate('{:08b}'.format(values)):
bit = int(bit)
@tyler-8
tyler-8 / Dockerfile
Created May 4, 2018 14:13
Docker Image for Python 3.6 and net-snmp installed (for EasySNMP usage).
FROM python:3.6
# Annoying sources for net-snmp, which is a dependency for easysnmp
RUN export DEBIAN_FRONTEND=noninteractive && \
export DEBIAN_RELEASE=$(awk -F'[" ]' '/VERSION=/{print $3}' /etc/os-release | tr -cd '[[:alnum:]]._-' ) && \
echo "remove main from /etc/apt/sources.list" && \
sed -i '/main/d' /etc/apt/sources.list && \
echo "remove contrib from /etc/apt/sources.list" && \
sed -i '/contrib/d' /etc/apt/sources.list && \
echo "remove non-free from /etc/apt/sources.list" && \
sed -i '/non-free/d' /etc/apt/sources.list && \
@tyler-8
tyler-8 / cf_netmiko.py
Created January 11, 2019 01:59
Example of concurrent futures with netmiko
#!/usr/bin/env python3
'''
Use processes and Netmiko to connect to each of the devices. Execute
'show version' on each device. Use concurrent futures built-in queue
to pass the output back to the parent process. Record the amount of
time required to do this.
'''
import concurrent.futures as cf
from datetime import datetime
@tyler-8
tyler-8 / parse_cflow_pcap.py
Last active April 13, 2020 20:36
A quick example of how to use pyshark to parse cflow data from a capture
import pyshark
from collections import defaultdict
FIELDS = (
"srcaddr",
"dstaddr",
"octets",
)
@tyler-8
tyler-8 / wordle_helper.py
Last active January 23, 2022 04:10
A stupid script to help you cheat. Don't do it, this was just an experiment.
"""
Uses the `words_alpha.txt` file from
https://github.com/dwyl/english-words/
"""
import random
from collections import Counter
from typing import Iterable, Tuple
def has_no_repeat_letters(word: str) -> bool:
import asyncio
def do_work_sync(seconds):
"""Wait 'seconds' using sync code"""
return asyncio.run(do_work_async(seconds, "sync"))
def organize_work_sync(work_inputs):
"""Execute multiple calls of the sync function"""
@tyler-8
tyler-8 / site_circuit_validation.py
Last active July 1, 2022 01:57
A Custom Validator for NetBox 3.0+ to prevent sites from being retired if they have circuits that aren't deprovisioning or decommissioned.
from extras.validators import CustomValidator
from circuits.models import Circuit
class SiteStatusCircuitValidator(CustomValidator):
"""
Prevent sites from being retired if they have circuits that aren't in deprovisioning or decommissioned status.
"""
def validate(self, site):
circuit_count = Circuit.objects.filter(terminations__site=site).exclude(