Yeah, sorry i should of thought about including the files in my original post but here they are:
PythonMenu.py is my main python menu script
__________________________________________________________________________________________________________________________________________
#This is the main script for component 2 of Programming for System Administrators.
#CM3123.
#Author - Adam Nayler.
#Student Number - 1803974.
#Initial Creation Date - 26/10/2018.
#!/usr/bin/env python3
#The first line imports the ConnectHandler from netmiko to allow for secure SSH connections by the script.
#import os, imports the os module for the scripts use.
#import sys, imports the sys module for the scripts use.
from netmiko import ConnectHandler
import os
import sys
import RoutingConfigurationScript
#This is a function to introduce the script to the user.
#'clear' will clear everythign off the screen that is currentely there to stop cluttering and increase tidyness.
def introduction():
os.system('clear') #Clear Screen.
print("\n\t\t\tWelcome to the assignment script for CM3123")
print("\n\t\t\tCreated by Adam Nayler")
print("\n\t\t\tStudent Number - 1803974")
input("\n\t\t\t\tPlease press enter to begin the script")
menu()
#This is a function for the main scripts menu.
def menu():
selection = ""
while selection == "":
os.system('clear') #Clear Screen.
print("\n\n\n")
print("\t\t\tPython3 Script By Adam Nayler\n\n\n")
print("\t\t\tStudent Number - 1803974\n\n\n")
print("\tPlease select a choice from the options below:\n\n")
print("\t1. Remotely Configure a Routing Device. ")
print("\t2. Remotely Configure a Switching Device. ")
print("\t3. Open Up A Sub-menu For Comparing Different Configuration Files. ")
print("\t4. Open Up A Sub-menu For Extracting Performance And Device Parameters From Different Network Devices. ")
print("\t5. Capture And Output Network Traffic. ")
print("\t6. Exit the Menu. ")
selection = input("\n\tPlease select the option you wish to run: ")
if selection == "1":
ConfigureRoutingDevices()
elif selection == "2":
ConfigureSwitchingDevices()
elif selection == "3":
ComparisonMenu()
elif selection == "4":
ExtractingDataMenu()
elif selection == "5":
CaptureAndOutputTraffic()
elif selection == "6":
exit()
else:
os.system('clear') #Clear Screen.
print("\n\n\n\t\t\tYou have entered and incorrect value. ")
print("\n\t\t\tPress the enter key to return to the main menu. ")
input ("")
#This is the function that connects to multiple routing devices and performs the required commands from the commands file.
def ConfigureRoutingDevices():
print("1")
#This is the function that connects to multiple switching devices and performs the required commands from the commands file.
def ConfigureSwitchingDevices():
print("2")
#Function to open up the sub-menu that will compare configuration files.
def ComparisonMenu():
selection = ""
while selection == "":
os.system('clear') #CLear Screen.
print("\n\n\n")
print("\t\t\tThis Is The Sub-menu For Comparing Different Configuration Files\n\n\n ")
print("\tPlease select a choice from the options below.\n\n ")
print("\t1. Compare the running configuration and startup configuration of a routing device. ")
print("\t2. Compare the running configuration of a routing device with an offline configuration. ")
print("\t3. Compare the startup configuration of a routing device with an offline configuration. ")
print("\t4. Exit back to the original menu. ")
selection = input("\n\tPlease select the option you wish to run: ")
if selection == "1":
CompareRunningWithStartUpConfig()
elif selection == "2":
CompareRunningWithLocalConfig()
elif selection == "3":
CompareStartUpWithLocalConfig()
elif selection == "4":
menu()
else:
os.system('clear') #Clear Screen.
print("\n\n\n\t\t\tYou have entered and incorrect value. ")
print("\n\t\t\tPress the enter key to return to the main menu. ")
input ("")
#Function to open up the sub-menu that will allow the user of the script to extrat a choice of performance and device parameters from a network device.
#The data then needs to be extracted to a MYSQL database within json format.
def ExtractingDataMenu():
selection = ""
while selection == "":
os.system('clear') #Clear Screen.
print("\n\n\n")
print("\t\t\tThis Is A Sub-menu For Extracting Performance And Device Parameters From Network Devices\n\n\n ")
print("\tPlease select a choice from the options below.\n\n ")
print("\t1. Extract the CPU utilisation.")
print("\t2. Extract the proccessor memory.")
print("\t3. Extract the I/O memory. ")
print("\t4. Extract the model of device.")
print("\t5. Extract the IOS version of device.")
print("\t6. Exit back to main menu.")
selection = input("\n\t Please select the option you wish to run: ")
if selection == "1":
ExtractDataCPU()
elif selection == "2":
ExtractDataProcessor()
elif selection == "3":
ExtractDataIO()
elif selection == "4":
ExtractDataModel()
elif selection == "5":
ExtractDataIOS()
elif selection == "6":
menu()
else:
os.system('clear') #Clear Screen.
print("\n\n\n\t\t\tYou have entered and incorrect value. ")
print("\n\t\t\tPress the enter key to return to the main menu. ")
input ("")
#This is the function to capture network traffic and continuosly output it to a file / files.
#The output will be sotred in pcap format.
def CaptureAndOutputTraffic():
print("5")
#These functions are the ones to be used within the comparison menu.
#This function compares a devices running configuration with its startup configuration.
def CompareRunningWithStartUpConfig():
print("Blank")
#This function compares a devices running configuration with a local, offline configuration.
def CompareRunningWithLocalConfig():
print("Blank")
#This function compares a devices startup configuration with a local, offline configuration.
def CompareStartUpWithLocalConfig():
print("Blank")
#These functions are the ones used within the extraction menu.
#Function to extract CPU data
def ExtractDataCPU():
print("Blank")
#Function to extract Proccessor Memory data
def ExtractDataProcessor():
print("Blank")
#Function to extract I/O memory
def ExtractDataIO():
print("Blank")
#Function to extract Device Model number
def ExtractDataModel():
print("Blank")
#Function to extra IOS number of device
def ExtractDataIOS():
print("Blank")
#This function allows the user to leave the script whenever they wish to.
def exit():
os.system('clear') #Clear Screen.
sys.exit('\n\n\tThank you for using this script. I hope it met your requirements.\n\n')
#This is the function for the main body of the script.
def main():
menu()
#This calls the script in order to start it.
main()
__________________________________________________________________________________________________________________________________________
RoutingConfigurationScript.py is the external script i am trying to import as a module into my python menu and activate after the user activates option one on the menu.
It would replace the command 'print("1")' that i currently have there as a placeholder.
__________________________________________________________________________________________________________________________________________
#Import getpass module for use in the script.
#Import the ConnectHnadler from netmkio module to allow the script to SSH.
from getpass import getpass
from netmiko import ConnectHandler
#This ask the user to enter their SSH username to create a variable.
#It then requests the user to enter their SSH password to create a second variable.
username = input('Please enter your SSH username: ')
password = getpass()
#This opens the file 'routercommands_file' for use in the script.
with open('routercommands_file') as f:
commands_list = f.read().splitlines()
#This opens the file 'routerdevices_file' for use in the script.
with open('routerdevices_file') as f:
devices_list = f.read().splitlines()
#This uses the information entered by the user and exterior files to bring forward the correct information when needed to SSH into the device.
#It displays the message 'Connecting to device...' followed by the current IP address of the network device it is accessing.
#It then uses the variables to connect.
for devices in devices_list:
print ('Connecting to device" ' + devices)
ip_address_of_device = devices
ios_device = {
'device_type': 'cisco_ios',
'ip': ip_address_of_device,
'username': username,
'password': password
}
#This finally connects to the device through ssh with ConnectHandler.
#Outputs the commands from the command file.
#Finally, it prints the commands it entered for the user to see.
net_connect = ConnectHandler(**ios_device)
output = net_connect.send_config_set(commands_list)
print (output)
__________________________________________________________________________________________________________________________________________
Thanks for attempting to explain it Larz60, i still don't fully understand but hopefully if someone is able to show me the solution specific to my scripts I'll be able to learn from that.
Thanks.