Skip to content

Instantly share code, notes, and snippets.

@midwire
Last active January 4, 2024 20:01
  • Star 53 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save midwire/a8846d93fae93a0b1045 to your computer and use it in GitHub Desktop.
[Reset routing table on OSX] #osx #devops #networking
#!/usr/bin/env bash
# Reset routing table on OSX
# display current routing table
echo "********** BEFORE ****************************************"
netstat -r
echo "**********************************************************"
for i in {0..4}; do
sudo route -n flush # several times
done
echo "********** AFTER *****************************************"
netstat -r
echo "**********************************************************"
echo "Bringing interface down..."
sudo ifconfig en1 down
sleep 1
echo "Bringing interface back up..."
sudo ifconfig en1 up
sleep 1
echo "********** FINALLY ***************************************"
netstat -r
echo "**********************************************************"
@TryTryAgain
Copy link

TryTryAgain commented Sep 26, 2022

You'll definitely want to make that a netstat -rn instead of just nestat -r ... I'm not sure why (I do have many other tun, ipv6, loop and link devices)

Running this script took me half an hour because of running netstat -r three times and scrolling/streaming very slowly each time. Whereas when using -rn it's instantaneous.

Also, if you need to change interfaces for clearing/defaulting back to wireless or in case your main isn't en1, mine was en0... this is how I modified your script:

#!/usr/bin/env bash
# Reset routing table on OSX
specificInt=$1
if [ -z "$specificInt" ]; then specificInt="en1"; fi

# display current routing table
echo "********** BEFORE ****************************************"
netstat -rn
echo "**********************************************************"

for i in {0..4}; do
  sudo route -n flush # several times
done

echo "********** AFTER *****************************************"
netstat -rn
echo "**********************************************************"

echo "Bringing interface down..."
sudo ifconfig $specificInt down
sleep 1
echo "Bringing interface back up..."
sudo ifconfig $specificInt up
sleep 1

echo "********** FINALLY ***************************************"
netstat -rn
echo "**********************************************************"

...so I run it like ./reset_routing_table_macos.sh en0 when I need to reset that device vs the default/wired en1 (or set the specificInt default to whatever you use more often, but allow for the ability to specify something other than default and not needing to modify the script).

@midwire
Copy link
Author

midwire commented Sep 26, 2022

@TryTryAgain Thanks for this. Yeah, the -n option tells it to avoid trying to resolve hostnames which will indeed make it much faster. I usually like to see the hostnames but, certainly, if you prefer speed your version is the right choice. I'll probably make that be an option at some point when I have some free minutes. Cheers!

@Cinemacloud
Copy link

This script was a lifesaver. For longest time I've tried to fix this constant issue and gave up with often needing to reboot or
spend endless time thinking there was another cause but I always suspected it was a lingering / dynamically misconfigured routing issue but flushing once or twice never fixed, or something else in the short logic above did the trick. THANK YOU!

@midwire
Copy link
Author

midwire commented Jan 4, 2024

@Cinemacloud glad it helped. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment