#!/usr/bin/env python3
## USOpen Tournament Switch Checker -- 2018.05.01
''' usopen.py
This script is being designed to provide the following automated tasks:
- ping check the router (import os)
- login check the router (import netmiko)
- determine if interfaces in use are up (import netmiko)
- Apply new configuration (import netmiko) # not yet built

The IPs and device type should be made available via an Excel spreadsheet

'''
import os

## pyexcel and pyexce-xls are required for our program to execute
# python3 -m pip install --user pyexcel
# python3 -m pip install --user pyexcel-xls
import pyexcel

# python3 -m pip install --user netmiko
from netmiko import ConnectHandler

## retrieve data set from excel
def retv_excel(par):
    d = {}
    records = pyexcel.iget_records(file_name=par) # create a record object that is an open excel sheet
    for record in records:
        d.update( { record['IP'] : record['driver'] } ) # adds a new IP and driver key:value pair to our dictionary
    return d # return the completed dictionary

## Ping router - returns True or False
def ping_router(hostname):

    response = os.system("ping -c 1 " + hostname)

    #and then check the response...
    if response == 0:
        return True
    else:
        return False


## Check interfaces - Issue "show ip init brief"
def interface_check(dev_type, dev_ip, dev_un, dev_pw):
    try:
        open_connection = ConnectHandler(device_type=dev_type, ip=dev_ip, username=dev_un, password=dev_pw)
        my_command = open_connection.send_command("show ip int brief")       
    except:
        my_command = "** ISSUING COMMAND FAILED **"
    finally:
        return my_command


## Login to router - SSH Check with Netmiko class ConnectHandler
def login_router(dev_type, dev_ip, dev_un, dev_pw):
    try:
        open_connection = ConnectHandler(device_type=dev_type, ip=dev_ip, username=dev_un, password=<PASSWORD>)
        return True        
    except:
        return False

def bootstrapper(ip_addr, dev_type, config_loc):
    conn = ConnectHandler(device_type=dev_type, ip=ip_addr, username="admin", password="<PASSWORD>")
    print("Sending config to device, this might take a minute...")
    try:
        conn.enable()
        conn.send_config_from_file(config_loc)
        return True
    except BaseException as e:
        print(e)
        return False

## Main function - This is the code that runs our functions
def main():

    ## Determine where *.xls input is
    file_location = str(input("\nWhere is the file location? "))

    ## Entry is now a local dictionary containing IP(key):driver(value)
    entry = retv_excel(file_location)

    ## Use a loop to check each device for SSH accessability
    print("\n***** BEGIN SSH CHECKING *****")
    for x in entry.keys():
        if login_router(str(entry[x]), x, "admin", "alta3"):
            print("\n\t**IP: - " + x + " - SSH connectivty UP\n")
        else:
            print("\n\t**IP: - " + x + " - SSH connectivty DOWN\n")

    ## Use a loop to check each device for ICMP responses
    print("\n***** BEGIN ICMP CHECKING *****")
    for x in entry.keys():
        if ping_router(x):
            print("\n\t**IP: - " + x + " - responding to ICMP\n")
        else:
            print("\n\t**IP: - " + x + " - NOT responding to ICMP\n")

    ## Use a loop to check each device for ICMP responses
    print("\n***** BEGIN SHOW IP INT BRIEF *****")
    for x in entry.keys():
        print("\n" + interface_check(str(entry[x]), x, "admin", "alta3"))

    ## Ask user if they want to upload a new config file

    new_conf = input("Do you want to upload a new config file? (y/n)")
    if new_conf[0].lower() == 'y':
        config = input("Where is the config file location?\n")
        ip = input("Enter the IPv4 Address of the device you'd like to configure: \n")
        driver = ""
        try:
            driver = entry[ip]
            if bootstrapper(ip, driver, config):
                print("Config successfully uploaded!")
            else:
                print("There was an error uploading the config...")
        except KeyError:
            print("Sorry that IP address was not in the device list...")
            
    else:
        print("Exiting Script")    

## Call main()
main()
