Skip to content

Instantly share code, notes, and snippets.

@h4ndzdatm0ld
h4ndzdatm0ld / globfindfile
Last active November 17, 2020 05:59
globfindfile #glob,file,find
def globfindfile(regex):
''' This function will simply locate a file in the DIR by passing the directory/regex value (ie:(*.log))
The returned value by calling the function is the file.
'''
try:
if len(glob.glob(regex)) == 0:
sys.exit(f"No {regex} file found.")
else:
for file in glob.glob(regex):
if len(glob.glob(regex)) > 1:
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print('Error: Creating directory. ' + directory)
@h4ndzdatm0ld
h4ndzdatm0ld / netconf
Last active May 18, 2020 23:48
NETCONF template
def netconfconn(nodeip,ncusername,ncpassword):
conn = manager.connect(host=nodeip,
port=830,
username=ncusername,
password=ncpassword,
hostkey_verify=False,
device_params={'name':'alu'})
return conn
@h4ndzdatm0ld
h4ndzdatm0ld / saveflle
Created May 18, 2020 23:49
Save contents to file
def saveFile(filename, contents):
''' Save the contents to a file in the PWD.
'''
try:
f = open(filename, 'w+')
f.write(contents)
f.close()
except Exception as e:
print(e)
@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}")
@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 / 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
def dictconvert(lst):
res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
return res_dct
@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 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