from netmiko import ConnectHandler
from getpass import getpass
import requests
import urllib3
import re
import time
import getpass
2. Next set up our input. this will allow us to select a "site" which will be a text file with all of our switch IP's that we are going to be logging into.print(" 1 = Site1 \n 2 = Site2 \n Select Site:")
site = input()
print(f"Site Selected: {site}")
print("----------------------------------")
print("----------------------------------")
print(" 1 = wired_l1_game_dur-3142-2 \n 2 = wired_l2_print_dur-3134-6 \n Select Role:")
role = input()
if campus == "1":
file = [line.strip() for line in open("site1.txt", 'r')]
if campus == "2":
file = [line.strip() for line in open("site2.txt", 'r')]
##### Add more roles to the print output line if you have more to add
#print(file[])
#file.close()
if role == "1":
policyrole = "wired_l1_game_dur-3142-2"
if role == "2":
policyrole = "wired_l2_print_dur-3134-6"
## Add more roles if you have more to add
print("Enter Username:")
username = input()
print("Enter Password:")
password = getpass.getpass()
creds = {"username": {username}, "password": {password}}
3. We need to be able to log into the switch, we do this with the plugin NetMiko with the Connection Handler, this will read the text file we have loaded and pull in the switch IP address for each line so we can check the switch interfaces. The program will run through each line from the text file until it reaches the end.for selectIP in file:
#print("Enter the Switch IP:")
ip_add = selectIP
print(ip_add)
session = requests.session()
net_connect = ConnectHandler(
device_type="aruba_procurve",
host=ip_add,
username=ausername,
password=apassword,
)
4. Here is where the ports are actually changed. We will send a command to the switch to show the port role, get the information then bounce each interface with the same role. The program will print out which switch it is connected to as well as the ports that were changed when you run the command... This will help you know which ports were changed as well as to go back and check if you wanted to.4. Here is where the ports are actually changed. We will send a command to the switch to show the port role get the information then bounce each on of those ports for that specific role.output = net_connect.send_command(f"show port-access clients role {policyrole}")
to1 = output.split("\n")
print(f"Ports that have role: {policyrole}:")
for to in to1 :
to = to[2:]
to = to.split(" ")[0]
if re.search("[\d]+/[\d]/[\d]+",to):
print (to)
to2 = to.split("/")
output1 = [f"interface {to2[0]}/{to2[1]}/{to2[2]}","shut"]
net_connect.send_config_set(output1)
time.sleep(1)
output2 = [f"interface {to2[0]}/{to2[1]}/{to2[2]}","no shut"]
net_connect.send_config_set(output2)
net_connect.send_config_set(f"wr mem")
net_connect.disconnect()
print(f"Logged out from Switch!")