Skip to content

Instantly share code, notes, and snippets.

@h4ndzdatm0ld
h4ndzdatm0ld / Dockerfile-base
Last active June 6, 2021 17:38
Common template as a base start for Dockerfile
#############
# Dependencies
# This base stage just installs the dependencies required for production
# without any development deps.
ARG PYTHON_VER=3.8
FROM python:${PYTHON_VER} AS base
WORKDIR /usr/src/app
@h4ndzdatm0ld
h4ndzdatm0ld / simple_excel
Created February 14, 2021 06:05
Load a workbook as a dictionary
def simple_excel(workbook, sheetname, keep_vba=False, data_only=True):
wb_obj = load_workbook(
filename=workbook, keep_vba=keep_vba, data_only=data_only, read_only=True
)
wsheet = wb_obj[sheetname]
data_key = []
for value in wsheet.iter_rows(values_only=True):
@h4ndzdatm0ld
h4ndzdatm0ld / norninr-1-example
Last active September 23, 2020 22:50
Nornir Walkthrough 1
def get_vrfcli(task, servicename):
''' Retrieve VRF from IOSXR
'''
vrf = task.run(netmiko_send_command, command_string=f"sh vrf {servicename} detail")
print_result(vrf)
def get_vprncli(task, servicename):
''' Retrieve VPRN from Nokia
'''
vprn = task.run(netmiko_send_command, command_string=f"show service id {servicename} base")
def csvbackup(REGEX, FOLDER):
""" Find any file with the REGEX pattern in the PWD
and send it to assigned FOLDER.
"""
try:
current = os.getcwd()
files = glob.iglob(os.path.join(current, REGEX))
DST_FLD = current + '/' + FOLDER
for bkup in files:
if os.path.isfile(bkup):
def get_credentials():
'''prompts user for password and verifies.'''
username = input('Enter Username: ')
password = None
while not password:
password = get_pass()
password_verify - get_pass('Re-enter Password')
if passowrd != password_verify:
print('Passwords do not match. Try again')
password = None
@h4ndzdatm0ld
h4ndzdatm0ld / logging
Created May 21, 2020 05:05
Logging netmiko
def netmiko_logging():
''' Netmiko Logging - This creates a Log file (NetmikoLog.txt) under a new 'Logging' folder.. Does not overwrite (a+).
Log must end in .txt file as the program won't allow two .log files in the CWD.
'''
create_folder('Logging')
open('Logging/NetmikoLog.txt', 'a+')
logging.basicConfig(filename='Logging/NetmikoLog.txt', level=logging.DEBUG)
logger = logging.getLogger("netmiko")
def dictconvert(lst):
res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
return res_dct
@h4ndzdatm0ld
h4ndzdatm0ld / arguments
Created May 20, 2020 16:46
argparse example
def get_arguments():
parser = argparse.ArgumentParser(description='Command Line Driven Utility To Enable NETCONF\
And MD-CLI on SROS Devices.')
parser.add_argument("-n", "--node", help="Target NODE IP", required=True)
parser.add_argument("-u", "--user", help="SSH Username", required=False, default='admin')
parser.add_argument("-p", "--port", help="NETCONF TCP Port", required=False, default='830')
args = parser.parse_args()
return args
@h4ndzdatm0ld
h4ndzdatm0ld / netconfbackup
Created May 19, 2020 21:30
Establish a netconf connection and create a backup/xml dict
def netcbackup(ip, NETCONF_USER, NETCONF_PASS):
''' This function will establish a netconf connection and pull the running config. It will write a temp file,
read it and convert the XML to a python dictionary. Once parsed, we'll pull the system name of the device
and create a folder structure by hostname and backup the running config.
'''
try:
# Now let's connect to the device via NETCONF and pull the config to validate
nc = netconfconn(ip, NETCONF_USER, NETCONF_PASS)
# Grab the running configuration on our device, as an NCElement.
@h4ndzdatm0ld
h4ndzdatm0ld / get_ip
Created May 18, 2020 23:50
Remove Subnet Mask from IP Addy
def get_ip_only(ipadd):
''' This function will use REGEX to strip the subnet mask from an IP/MASK addy.
'''
try:
ip = re.sub(r'/.+', '', str(ipadd))
return ip
except Exception as e:
print(f"Issue striping subnetmask from {ipadd}, {e}")