Skip to content

Instantly share code, notes, and snippets.

@c4710n
Created November 8, 2023 03:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c4710n/1c4377712339a896464ab64767e6ba8f to your computer and use it in GitHub Desktop.
Save c4710n/1c4377712339a896464ab64767e6ba8f to your computer and use it in GitHub Desktop.
Fix macOS corrupted routing table
#!/usr/bin/env bash
#
# ## The Problem
#
# Can't assign requested address
#
# It occurs when macOS's TCP stack routing table has become corrupted.
#
# ## Reproducing
#
# I have experienced this issue frequently when using Surge or ClashX:
#
# * changing networks while having the Enhanced Mode enabled.
# * putting the device to sleep and waking it up to a different network.
#
# Related problems:
#
# * https://discussions.apple.com/thread/3792069
# * http://codefromabove.com/quickies/osx-cant-assign-requested-address-code49/
# * https://www.expressvpn.com/support/troubleshooting/log-items/unable-to-connect-cant-assign-requested-address-code49/
#
# ## The Solution
#
# To fix it, the routing table should be flushed. There are two
# available methods:
#
# * reboot - seems stupid, because I have to reopen all the existing
# applications and documents.
# * use this script - preferred.
#
PATH=/bin:/sbin
# This is the outbound interface to Internet.
# Use ifconfig -L to figure out which interface corresponds to WiFi/Ethernet:
#
# * en0 is Ethernet, typically.
# * en0 is WiFi on MacBook without hardware Ethernet.
#
INTERFACE=en0
echo "taking $INTERFACE down..."
ifconfig "$INTERFACE" down
sleep 2
echo "flushing the routing table..."
route -n flush
sleep 2
echo "taking $INTERFACE up..."
ifconfig "$INTERFACE" up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment