Skip to content
Snippets Groups Projects
Select Git revision
  • 399597949a749d6ecf90f907a29d720029364f64
  • main default protected
  • threads
3 results

ConfigChecker.py

Blame
  • ConfigChecker.py 7.87 KiB
    from urllib3 import exceptions  # Prevent SSL Self-Signed Certificate Error
    import requests  # Creation Of New Object
    import urllib3
    from netmiko import (ConnectHandler,NetmikoTimeoutException)  # SSH Connection
    import socket  # DNS Lookup
    import time  # For Waiting
    import CampusSelector
    import SwitchCommands
    
    
    def main(ArubaUsername, ArubaPassword):
        # Preparatory Work for SSH Connection
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)  # Disable Certificate Warning
        interfaces = {}  # Initialize the interface variable
        current_interface = ""  # Initialize Current Interface to make sure it isn't null
        port_configuration = ['vlan access 1', 'enable', 'enable']
        exception = ['trunk allowed 2000', 'trunk allowed 1999']
        int_changes = []
    
        # Get the Campus or Switch IP Address
        campus = CampusSelector.Campus_Selector()
        ip = campus[1]
        campus_selection = campus[2]
        campus_file = campus[0]
    
        # Single Switch Only
        if campus_selection == "6":
            hostname = socket.getfqdn(ip)
            print('Current Switch:', hostname, ":", ip)
            missing = []
    
            # Create Session Object
            #session = requests.session()
    
            # Create SSH Connection
    
            ssh = ConnectHandler(
                device_type="aruba_procurve",
                host=ip,
                username=ArubaUsername,
                password=ArubaPassword,
            )
    
            # Find The Ports With Deviations
            try:
                cli = ssh.send_command(f"show run int")
                parse = cli.split("\n")
                for line in parse:
                    for x in exception:
                        if x in line:
                            print("An Exception has been found, skipping port:", current_interface,
                                  "Either exception VLAN or trunk port")
                            missing.clear()
                            continue
                    if line.split()[0].strip() == "interface":
                        if len(missing) > 0:
                            print("The following Config Was Missing From Interface:", current_interface, missing)
                            print(
                                "Would you like to reset this interface back to normal configuration? \n 1) Yes "
                                "\n 2) No")
                            missing.clear()
                            reset = input()
                            if reset == "1":  # Reset the Interface
                                print('Resetting Port to Default: ', current_interface)
                                SwitchCommands.defaultport(ssh, current_interface)  # Reset Port
                                int_changes.append(current_interface)
                            if reset == "2":  # Skip Resetting the Interface
                                print("Skipping Interface Reset")
    
                        # Set some Variables
                        current_interface = line.split()[1].strip()
                        port = current_interface.split("/")[-1]
    
                        # Global Ignore Conditions
                        if port == '49' or port == '50' or port == '51' or port == '52':  # Ignore SFP Ports
                            continue
                        if port == 'vlan1' or port == "vlan1999" or "lag" in port:  # Ignore Local Interfaces
                            print("Local Interface, Skipping.", current_interface)
                            continue
                        for m in missing:
                            interfaces[current_interface].append(m)
                        for x in port_configuration:
                            missing.append(x)
                    else:
                        for command in missing:
                            if line.strip() == command.strip():
                                missing.pop(missing.index(command.strip()))
    
            finally:
                print("Logging Out From Switch", hostname, ip)
                ssh.disconnect()
                time.sleep(1)
    
        # Read from File
        if campus_selection != "6":
            with open(campus_file) as file:
                while line := file.readline().rstrip():
                    ip = line
                    hostname = socket.getfqdn(ip)
                    print('Current Switch:', hostname, ":", ip)
    
                    missing = []
                    # Create Session Object
                    session = requests.session()
    
                    # Create SSH Connection
                    try:
                        ssh = ConnectHandler(
                            device_type="aruba_procurve",
                            host=ip,
                            username=ArubaUsername,
                            password=ArubaPassword,
                        )
    
                        # Find The Ports With Deviations
                        try:
                            cli = ssh.send_command(f"show run int")
                            parse = cli.split("\n")
                            for line in parse:
                                for x in exception:
                                    if x in line or "trunk allowed" in line:
                                        print("An Exception has been found, skipping port:", current_interface, "Either exception VLAN or trunk port")
                                        missing.clear()
                                        continue
                                if line.split()[0].strip() == "interface":
                                    if len(missing) > 0:
                                        print("The following Config Was Missing From Interface:", current_interface, missing)
                                        print(
                                            "Would you like to reset this interface back to normal configuration? \n 1) Yes "
                                            "\n 2) No")
                                        missing.clear()
                                        reset = input()
                                        if reset == "1":  # Reset the Interface
                                            print('Resetting Port to Default: ', current_interface)
                                            SwitchCommands.defaultport(ssh, current_interface)  # Reset Port
                                            int_changes.append(current_interface)
                                        if reset == "2":  # Skip Resetting the Interface
                                            print("Skipping Interface Reset")
    
                                    # Set some Variables
                                    current_interface = line.split()[1].strip()
                                    port = current_interface.split("/")[-1]
    
                                    # Global Ignore Conditions
                                    if port == '49' or port == '50' or port == '51' or port == '52':  # Ignore SFP Ports
                                        continue
                                    if port == 'vlan1' or port == "vlan1999" or "lag" in port:  # Ignore Local Interfaces
                                        print("Local Interface, Skipping.", current_interface)
                                        continue
                                    for m in missing:
                                        interfaces[current_interface].append(m)
                                    for x in port_configuration:
                                        missing.append(x)
                                else:
                                    for command in missing:
                                        if line.strip() == command.strip():
                                            missing.pop(missing.index(command.strip()))
    
                        finally:
                            print("Interfaces that changed:", int_changes)  # Print Interface Change Summary
                            int_changes.clear()  # Clear the List For Next Switch
                            print("Logging Out From Switch", hostname, ip)
                            ssh.disconnect()
                            time.sleep(1)
                    except (NetmikoTimeoutException) as error:
                        print("Switch is not responding----->  " + ip)
                        print("Switch is not responding----->  " + ip)
                        print("Switch is not responding----->  " + ip)