Skip to content

digital-md/Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Develop Algorithms for File Updates

Description

As a security professional, I am going to update a file for a healthcare company that identifies employees who can access restricted content. The content of the files is on a file called “allow_list.txt” listing the IP addresses. The employees are either restricted, allowed or removed based on their IP addresses. I will be using Python code to create an algorithm to check to see if any employees are to be removed from the allowed list. The # symbol indicates the comments made that direct the process.

Open the file that contains the allow list

First, I will open “allow_list.txt” and assign it as a string to the import_file variable.

image

Second, I used the ‘with’ ‘open’ statements to open the allowed list file to read it and view the IP addresses stored in the file. I used with open(“import_file”, “r”) as file: , there are two parameters, one locates the file to import and the other in this case is the “r” which is for me to read the file. Additionally, as is used to assign a new variable named file. This file will store the output of open() while I work with it.

image

Read the file contents

image

When I use the open() function that includes “r” for read. The read() method will convert the file IP addresses into a string and allow me to use it. I used read() to the file variable in the with statement then assigned a string output to the IP addresses variable. This code will allow reading of the allowed_list.txt file and allow me to read and extract data later.

Convert the string into a list

For me to remove the IP addresses from the allowed_list. I needed to parse it by using .split() to convert the string IP addresses into a list.

image

When .split() is used it appends and converts the contents of that string to a list. The reason we split IP addresses into this list is because it will make it easier to remove IP addresses in the alllowed_list. The algorithm .split() function will take data stored in IP addresses and convert the string into list of IP addresses. Then I will reassign it back to the variable IP addresses.

Iterate through the remove list

Iterating through the IP addresses in this algorithm is key to reviewing remove_list. Therefore, I needed to create a for loop

image

In for loop it will repeat the code by applying specific statements to all elements in sequence. It starts the loop with for and is followed by a loop variable named element. The in keyword directs the iterate through sequences of the IP addresses and assigns a value to the variable element.

Remove IP addresses that are on the remove list

The algorithm I made requires removing the IP addresses from the allow list, which is also in the remove list. I used this code to achieve this.

image

First in the for loop I needed to create a conditional to evaluate if the loop variable element would be found in the list of IP addresses. I applied .remove() to IP addresses and passed the loop to element as the argument so that the IP addresses in the remove list would be removed from IP addresses.

Update the file with the revised list of IP addresses

I updated the allow list file with the revised list of IP addresses. First I had to convert the list back into a string. I used .join() for this.

image

When using .join() it combines all items in an iterable into a string. When it is applied to a string containing characters it will separate the elements in the iterable once it has joined a string. In the algorithm, I used .join()to create the string from the list in ip_addresses so it passes as an argument to the .write() method when writing to this file "allow_list.txt". I used ("\n") a string to be a separator that instructs the code to place the elements on a new line. Then, I used the with statement and .write()to update the file.

image

In this instance, I employed the “w” argument in conjunction with the open() function within my with statement. This argument signifies my intention to overwrite a file’s contents. When the “w” argument is used, the .write() function can be invoked within the with statement’s body. The .write() function overwrites a specified file with string data, replacing any pre-existing content. In this scenario, my goal was to overwrite the “allow_list.txt” file with the updated allow list in string format. Consequently, any IP addresses removed from the allow list will no longer have access to restricted content. To overwrite the file, I attached the .write() function to the file object, identified as ‘file’ in the with statement. I input the ip_addresses variable as the argument, indicating that the data in this variable should replace the contents of the file specified in the with statement.

Summary

I created an algorithm that eliminates IP addresses listed in a variable named remove_list from the “allow_list.txt” file, which contains approved IP addresses. This process involves opening the file, transforming its content into a string for reading, and then converting this string into a list that is stored in the ip_addresses variable. Next, I loop through the IP addresses in remove_list. During each iteration, I check if the current IP address exists in the ip_addresses list. If it does, I use the .remove() method to delete it from ip_addresses. Finally, I use the .join() method to convert ip_addresses back into a string. This allows me to overwrite the “allow_list.txt” file with the updated list of IP addresses.

--!>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published