Skip to content

Instantly share code, notes, and snippets.

@spdkils
Created January 16, 2018 08:06
Show Gist options
  • Save spdkils/316289ee34880982b927785cda1a1d53 to your computer and use it in GitHub Desktop.
Save spdkils/316289ee34880982b927785cda1a1d53 to your computer and use it in GitHub Desktop.
Ugly Function to flip ACE statements. (Usable to see if outbound has properly inverted statements.
import re
def flipACE(ace: str) -> list:
search = r'(?:host \d+\.\d+\.\d+\.\d+|\d+\.\d+\.\d+\.\d+ \d+\.\d+\.\d+\.\d+|any)'
endofline = r'(?:established|log-input|log|$)'
remark = r'^ ?remark'
if re.search(remark, ace):
return ace.split()
if re.search(' reflect ', ace):
return '! should not be flipped reflexive'.split()
end = re.search(endofline, ace)
srcthendest = re.finditer(search, ace)
source = srcthendest.__next__()
destination = srcthendest.__next__()
aceAction = slice(0, source.start())
sourceAndPorts = slice(source.start(), destination.start())
destinationAndPorts = slice(destination.start(), end.start())
# sourceports = slice(source.end(), destination.start())
# destinationports = slice(destination.end(), end.end())
return [ace[aceAction].strip(),
ace[destinationAndPorts].strip(),
ace[sourceAndPorts].strip()
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment