Removing addresses from an allow list with Python
A short Python script that reads a text allow list, removes a set of unauthorized IPs and writes the cleaned list back, and what it would need before it controlled anything real.
Contents
The problem
Around 1990 Wietse Venema, at the Eindhoven University of Technology, was trying to watch an intruder who kept coming back to the university’s machines. The tool he wrote for it, TCP Wrapper, sat in front of network services and decided who could reach them by reading two plain text files, /etc/hosts.allow and /etc/hosts.deny. Versions of those files still exist on Linux systems, and the idea outlived them everywhere: a list of addresses, kept as text, that says who may connect.
A list like that is only as good as the last edit to it. Somebody adds an address in a hurry, somebody else removes the wrong line, and nothing complains until a user is locked out or a stranger is let in.
A simulated environment here keeps an allow list of IP addresses in a text file, allow_list.txt. Periodically, some addresses need to come off it. By hand, that is error-prone the moment the list runs past ten entries, and the operation belongs in code that can be reviewed and kept under version control. This is a short script that does the job.
Reading the file
import_file = "allow_list.txt"
with open(import_file, "r") as file:
ip_addresses = file.read()
Opening in read mode ("r") gives the raw text as one string. The with block closes the file when the block ends, including when an exception ends it early, which is the reason to use it over a bare open().
Splitting into a list
ip_addresses = ip_addresses.split("\n")
split("\n") turns the multi-line string into a list of lines. Each line is an IP, so each list element is one address. If the file ends with a newline, the last element is an empty string. That does no harm, and the join at the end puts the newline back.
Removing the unauthorized entries
remove_list = ["10.0.0.5", "172.16.0.3"]
for element in remove_list:
if element in ip_addresses:
ip_addresses.remove(element)
For each IP in remove_list, the loop checks whether it appears in the allow list and removes it if so. The loop walks remove_list and changes ip_addresses, so it never modifies the list it is iterating over. .remove() deletes the first occurrence only; that is fine here because IPs are unique in the source file.
If duplicates were possible, a list comprehension would be safer: ip_addresses = [ip for ip in ip_addresses if ip not in remove_list].
Matching is exact, character for character. A line with a stray trailing space will not match its address and will survive the cleanup, which is one more reason the input needs checking.
Writing back
ip_addresses = "\n".join(ip_addresses)
with open(import_file, "w") as file:
file.write(ip_addresses)
Writing the cleaned list back.
"\n".join() reverses the earlier .split("\n"), putting the list back into the file’s original shape. Opening in "w" mode truncates the file before writing, so the result fully replaces the previous contents.
That truncation is also the script’s weak point. The file is emptied first and written second, and if the process dies in between, the allow list is left empty or half written. The write is not atomic.
What this is actually useful for
The script is small. The pattern is what matters: read a structured config file, change it in code, write it back. The same shape applies to firewall config, DNS zone files, /etc/hosts, allow lists, deny lists, anything text-based.
What is missing here, and would matter in production:
- An atomic write: write the new list to a temporary file in the same directory, then swap it into place with
os.replace(), so the file on disk is always either the old list or the new one - File locking, so two runs cannot race each other
- A backup copy before overwriting, so a bad input can be undone
- Validation that the input file actually looks like an IP list before changing it
- A log of what was removed, with timestamps, for audit
For a self-contained exercise this script is enough. For something that controls who can reach a system, those additions are the difference between automation and automation that will one day delete the wrong thing.
Sources
- Venema, Wietse. “TCP WRAPPER: Network Monitoring, Access Control, and Booby Traps.” Proceedings of the 3rd UNIX Security Symposium, USENIX, 1992.
- Python Software Foundation. Python documentation for
open(),str.split(),list.remove()andos.replace().



