from netmiko import ConnectHandler
from getpass import getpass
import requests
import urllib3
import socket
import re 
import time
import getpass


urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

###########################################################################
print(" 1 = UNL-City \n 2 = UNL-East \n 3 = UNO \n 4 = UNK \n Select Campus:")
campus = input()
print(f"Campus Selected: {campus}")
print("----------------------------------")
if campus == "1":
    file = [line.strip() for line in open("unlCitySwitches-cx.txt", 'r')]
if campus == "2":
    file = [line.strip() for line in open("unlEastSwitches-cx.txt", 'r')]
if campus == "3":
    file = [line.strip() for line in open("unoSwitches-cx.txt", 'r')]
if campus == "4":
    file = [line.strip() for line in open("unkSwitches-cx.txt", 'r')]
    #print(file[])
    #file.close()

##########################################################################
print("Enter Username:")
Ausername = input()
print("Enter Password:")
Apassword = getpass.getpass()
creds = {"username": {Ausername}, "password": {Apassword}}

#policyrole = "wired_l1_game_dur-3142-2"


for selectIP in file:
    ip_add = selectIP
    hostname = socket.getfqdn(ip_add)
    print(f"Login  from: {hostname} {ip_add}")
    session = requests.session()
    

# create sessions object
    def get_logs(self):
        try:
            # Login to API and set initial Variables
            login = session.post(f"https://{self}/rest/v1/login", data=creds, verify=False)
            transitions = 5000
            vsfMember = 1
            Switch = 1
            SwitchPort = 1

            # Check to see how many switches are in stack and convert to string
            get_vsf = session.get(f"https://{self}/rest/v10.04/system/vsf_members?attributes=id&depth=2&selector=status&count=true")
            vsfMember = re.findall(r'\d+',f"{get_vsf.json()}")
            str_vsfMember = str(vsfMember[0])

            #Run Program on all switches and ports to find High link trasitions.
            while Switch <= int(str_vsfMember[0]):
                while SwitchPort < 49:
                    get_log = session.get(f"https://{self}/rest/v10.04/system/interfaces/{Switch}%2F1%2F{SwitchPort}?attributes=link_resets&depth=2&selector=statistics")
                    LinkTransitions = re.findall(r'\d+',f"{get_log.json()}")
                    str_LinkTransitions = str(LinkTransitions[0])
                    if transitions <= int(str_LinkTransitions):
                        print(f"Port: {Switch}/1/{SwitchPort} {str_LinkTransitions}")
                    SwitchPort += 1  
                Switch += 1
            
               
            logout = session.post(f"https://{self}/rest/v1/logout")
            print(f"Logout from: {hostname} {ip_add}")
        except requests.exceptions.RequestException as e:
            print("Switch is not responding----->  " + ip_add)
            print("Switch is not responding----->  " + ip_add)
            print("Switch is not responding----->  " + ip_add)         



    ###############################################################################
    # Starts the program at def get_logs(self)
    get_logs(ip_add)