This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def createFolder(directory): | |
try: | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
except OSError: | |
print('Error: Creating directory. ' + directory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dictconvert(lst): | |
res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)} | |
return res_dct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer