Oct-28-2018, 08:12 PM
I am writing a script that accesses three different switches one after the other and adds certain commands to them.
I then want to save the start up config and running config to separate files and wasn't sure if my script for the last section is right or not or close to being right.
I then want to save the start up config and running config to separate files and wasn't sure if my script for the last section is right or not or close to being right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#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 script has been turned into one big function so that it can be correctly imported as a module to the main script. def SwitchingScript(): #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 ( 'switchcommands_file' ) as f: commands_list = f.read().splitlines() #This opens the file 'routerdevices_file' for use in the script. with open ( 'switchdevices_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) return (output) output_run = ssh( "show running-config" ) output_start = ssh( "show startup-config" ) #Print (oputput_run) #Print (output_start) #Open up file, file_run.txt file_run = open ( "file_run.txt" , "a" ) #Save output_run to the file. print (outputrun.decode( 'ascii' ), file = file_run) #Open up file, file_start.txt file_start = open ( "file_start.txt" , "a" ) #Save output_start to the file. print (output_start.decode( 'ascii' ), file = file_start) #Close the files after they have been amended. file_run.close() file_start.close() |