Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting to files.
#1
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.

#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()
Reply
#2
change:
    #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()
To:
with open("file_run.txt", "a") as fp:
    fp.write(outputrun.decode('ascii'))

with open("file_start.txt", "a") as fp:
    fp.write(output_start.decode('ascii'))
Using the 'with' statement has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. so you don't need to add a close statement.

I noticed you are using mode 'a'.
This will append to the file each time, so old data is kept. I expect that this is what you want, but just a heads up.
Reply
#3
What does the as fp and fp.write do?

Yeah ammend is what I am looking to do, thanks.
Reply
#4
fp is a 'file pointer' or 'file handle' (in essence same thing). It can be named anything you want.
once defined, all items pertaining to I/O object (specifically file I/O, see Input and Output 7.2: https://docs.python.org/3/tutorial/inputoutput.html and File Objects: https://docs.python.org/3/c-api/file.html ).

once defined, all methods are available by using fp (depending on how opened, or if opened)
such as fp.tell() which returns the current position of the file read/write pointer within the file

tell - Give position of file pointer (in file) relative to
Reply
#5
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Errors when extracting files from ZIPs tester_V 3 5,717 Mar-10-2021, 10:33 PM
Last Post: tester_V
  Extracting information from .xlsx files hobbyist 0 1,600 Jan-06-2021, 07:20 PM
Last Post: hobbyist
  Extracting data from multiple txt files Emmanouil 7 5,142 Aug-25-2019, 10:24 PM
Last Post: Emmanouil
  Help extracting comment data from multiple zip files SoulsKeeper 10 6,153 Sep-10-2018, 10:33 AM
Last Post: SoulsKeeper

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020