diff --git a/PortFlipper.py b/PortFlipper.py new file mode 100644 index 0000000000000000000000000000000000000000..78dcd2ade775e3d0303024dc59ee48053453fad1 --- /dev/null +++ b/PortFlipper.py @@ -0,0 +1,108 @@ +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): + urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # Disable Certificate Warning + + # Get the Campus or Switch IP Address + campus = CampusSelector.Campus_Selector() + ip = campus[1] + campus_selection = campus[2] + campus = campus[0] + + # Get the Role we want to bounce + role = CampusSelector.Role_Selector() + + if campus_selection == "6": + hostname = socket.getfqdn(ip) + print('Current Switch:', hostname, ":", ip) + + # Create Session Object + session = requests.session() + + # Create SSH Connection + ssh = ConnectHandler( + device_type="aruba_procurve", + host=ip, + username=ArubaUsername, + password=ArubaPassword, + ) + + try: + cli = ssh.send_command(f"show port-access clients") + parse = cli.split("\n") + for line in parse: + line = line.lstrip() + if role in line: + interface = line.split(" ")[0] + if interface == 'd': + interface = line.split(" ")[1] + print("Non-Tunneled Device, Skip:", interface) + else: + interface = line.split(" ")[0] + # int_config = [f'interface {interface}', 'shut'] + SwitchCommands.shutport(ssh, interface) + # config_send = ssh.send_config_set(int_config) + print("Sending Command: ", interface, "\n Port Shutdown - Waiting 2 Seconds") + # int_config = [f'interface {interface}', 'no shut'] + SwitchCommands.noshutport(ssh, interface) + # config_send = ssh.send_config_set(int_config) + print("Sending Command: ", interface, "\n Flipped - Waiting 1 Second For Next Port") + finally: + print("Logging Out From Switch", hostname, ip) + ssh.disconnect() + time.sleep(1) + + if campus_selection != "6": + with open(campus) as file: + while line := file.readline().rstrip(): + ip = line + hostname = socket.getfqdn(ip) + print('Current Switch:', hostname, ":", ip) + # Create Session Object + session = requests.session() + + # Create SSH Connection + try: + ssh = ConnectHandler( + device_type="aruba_procurve", + host=ip, + username=ArubaUsername, + password=ArubaPassword, + ) + try: + cli = ssh.send_command(f"show port-access clients") + parse = cli.split("\n") + for line in parse: + line = line.lstrip() + if role in line: + interface = line.split(" ")[0] + if interface == 'd': + interface = line.split(" ")[1] + print("Non-Tunneled Device, Skip:", interface) + else: + interface = line.split(" ")[0] + # int_config = [f'interface {interface}', 'shut'] + SwitchCommands.shutport(ssh, interface) + # config_send = ssh.send_config_set(int_config) + print("Sending Command: ", interface, "\n Port Shutdown - Waiting 2 Seconds") + # int_config = [f'interface {interface}', 'no shut'] + SwitchCommands.noshutport(ssh, interface) + # config_send = ssh.send_config_set(int_config) + print("Sending Command: ", interface, "\n Flipped - Waiting 1 Second For Next Port") + + finally: + print("Logging Out From Switch", hostname, ip) + ssh.disconnect() + except (NetmikoTimeoutException) as error: + print("Switch is not responding-----> " + ip) + print("Switch is not responding-----> " + ip) + print("Switch is not responding-----> " + ip) +