So I had to do some ACL’s quick for a disaster recovery for a client and seeing that manually per request i had thousand of lines of output, I decided to make a little script.
To use this scripts you just need to populate the 2 text file “source.txt and destination.txt” and execute the script.
You can make a package ( .exe ) specially if needed to be done in a prod environment with the proper authorization. See my other page for how to
http://www.patrickdenis.biz/blog/python-packaging/
https://pypi.org/project/pyinstaller/
For some reason worpress wont accept the indent line code … to be fix, for now ask chatgpt 😉
import re
# Source ip list
f = open(“Source.txt”, “r”) # if not in current path you need to add it
source_data = f.read()
# Destination ip list
f = open(“Destination.txt”, “r”) # if not in current path you need to add it
destination_data = f.read()
# Split the data into lists
source_list = source_data.strip().split(“\n”)
destination_list = destination_data.strip().split(“\n”)
#print(source_list)
#print(destination_list)
#Generate ACLs for each combination of source and destination
acl_list_IN = []
acl_list_OUT = []
for source in source_list:
source_ip = source.strip()
#print(source_ip)
for destination in destination_list:
acl_IN = f”permit ip host {source_ip} host {destination}”
if re.search(“0.0.0.”,acl_IN):
acl_IN = f”permit ip host {source_ip} {destination}”
acl_list_IN.append(acl_IN)
acl_OUT = f”permit ip host {destination} host {source_ip}”
if re.search(“0.0.0.”,acl_OUT):
acl_OUT = f”permit ip {destination} host {source_ip}”
acl_list_OUT.append(acl_OUT)
#Export the results to for the ACL_IN
result_ACL_IN = ‘RESULT_ACL_IN.txt’
with open(result_ACL_IN, ‘w’) as result_file1:
result_file1.write(“conf t”+’\n’+”ip access-list extended VLAN-ACL-IN”+”\n\n”)
for acl1 in acl_list_IN:
result_file1.write(acl1 + ‘\n’)
#Export the results to for the ACL_OUT
result_ACL_OUT = ‘RESULT_ACL_OUT.txt’
with open(result_ACL_OUT, ‘w’) as result_file2:
result_file2.write(“conf t”+’\n’+”ip access-list extended VLAN-ACL-Out”+”\n\n”)
for acl2 in acl_list_OUT:
result_file2.write(acl2 + ‘\n’)
#result_ACL_IN
#result_ACL_OUT