Last active
December 10, 2019 21:03
-
-
Save antlauzon/236bb04f850825fcad5f5dbe428b6c25 to your computer and use it in GitHub Desktop.
Heavy Handed MacOS Route Nuking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import re | |
import shlex | |
import subprocess | |
LIST_ROUTES="/usr/sbin/netstat -nr" | |
IFCONFIG="/sbin/ifconfig" | |
DELETE_LINK="/sbin/route -n delete {destination} -link {linkno}" | |
DELETE_NET="/sbin/route -n delete -ifscope {netif} -net {destination}" | |
DELETE_HOST="/sbin/route -n delete -ifscope {netif} {destination}" | |
DELETE_HOST_NONETIF="/sbin/route -n delete {destination}" | |
DELETE_HOST_INET6="/sbin/route -n delete -inet6 -ifscope {netif} {destination}" | |
DELETE_HOST_INET6_NONETIF="/sbin/route -n delete -inet6 {destination}" | |
DELETE_INET6_ADDR="/sbin/ifconfig {iface} inet6 {inet6_addr} delete" | |
INET4_DELIMITER="Internet:" | |
INET6_DELIMITER="Internet6:" | |
LOCAL_SUBNET='192.168.39' | |
p = subprocess.Popen(shlex.split(LIST_ROUTES), | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
so, se = p.communicate() | |
r = p.returncode | |
inet4_routes = [] | |
inet6_routes = [] | |
read_route = False | |
inet4_vals = [ | |
'destination', | |
'gateway', | |
'flags', | |
'refs', | |
'use', | |
'netif', | |
'expire' | |
] | |
inet6_vals = [ | |
'destination', | |
'gateway', | |
'flags', | |
'netif', | |
'expire' | |
] | |
inet_routes = inet4_routes | |
inet_vals = inet4_vals | |
INET6_LINE_RE = re.compile('inet6') | |
IFACE_LINE_RE = re.compile('^([\w|\d]+):.*') | |
ROUTE_SKIP_LINE_RE = re.compile("(Internet6?)|Destination|Routing tables|^$.*") | |
for line in [l.strip() for l in so.decode('utf-8').split('\n')]: | |
m = ROUTE_SKIP_LINE_RE.match(line) | |
if m: | |
if m.group(1) == 'Internet6': | |
inet_routes = inet6_routes | |
inet_vals = inet6_vals | |
continue | |
route = [e.strip() for e in line.split()] | |
inet_routes.append(dict(zip(inet_vals, route))) | |
for route in inet4_routes: | |
cmd = 'true' | |
if 'link' in route['gateway']: | |
if route['destination'] == '255.255.255.255/32': | |
params = { | |
'destination': route['destination'], | |
'netif': route['netif'] | |
} | |
cmd = DELETE_HOST.format(**params) | |
p = subprocess.Popen(shlex.split(cmd)) | |
so, se = p.communicate() | |
print(so) | |
print(se) | |
elif LOCAL_SUBNET in route['destination'] or \ | |
LOCAL_SUBNET in route['gateway'] : | |
continue | |
elif len(route['destination'].split('.')) == 4: | |
params = { | |
'destination': route['destination'], | |
'linkno': int(route['gateway'].split('#')[1]) | |
} | |
cmd = DELETE_LINK.format(**params) | |
p = subprocess.Popen(shlex.split(cmd)) | |
so, se = p.communicate() | |
print(so) | |
print(se) | |
else: | |
cmd = DELETE_NET.format(**route) | |
print("DELETE_NET: {}".format(cmd)) | |
p = subprocess.Popen(shlex.split(cmd)) | |
so, se = p.communicate() | |
print(so) | |
print(se) | |
cmd = DELETE_HOST_NONETIF.format(**route) | |
p = subprocess.Popen(shlex.split(cmd)) | |
so, se = p.communicate() | |
print(so) | |
print(se) | |
for route in inet6_routes: | |
cmd = DELETE_HOST_INET6.format(**route) | |
p = subprocess.Popen(shlex.split(cmd)) | |
so, se = p.communicate() | |
print(so) | |
print(se) | |
cmd = DELETE_HOST_INET6_NONETIF.format(**route) | |
p = subprocess.Popen(shlex.split(cmd)) | |
so, se = p.communicate() | |
print(so) | |
print(se) | |
p = subprocess.Popen(shlex.split(IFCONFIG), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
so, se = p.communicate() | |
lines = so.decode('utf-8').split('\n') | |
iface = 'en0' | |
for line in [l.strip() for l in lines]: | |
iface_match = IFACE_LINE_RE.match(line) | |
if iface_match: | |
iface = iface_match.group(1) | |
inet6_match = INET6_LINE_RE.match(line) | |
if inet6_match: | |
inet6_addr = line.strip().split()[1] | |
print(inet6_addr) | |
cmd = DELETE_INET6_ADDR.format(iface=iface, inet6_addr=inet6_addr) | |
print(cmd) | |
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
dso, dse = p.communicate() | |
print(dso) | |
print(dse) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment