Skip to content

Instantly share code, notes, and snippets.

@bradley219
Last active August 29, 2015 14:15
Show Gist options
  • Save bradley219/a73ea7d0db718f973d84 to your computer and use it in GitHub Desktop.
Save bradley219/a73ea7d0db718f973d84 to your computer and use it in GitHub Desktop.
#!/bin/bash
function new_link ()
{
if [[ -n $LINK ]] && [[ -n $INET ]] && [[ -n $ACTIVE ]]; then
echo "$LINK"
fi
LINK=`expr "$1" : '\(.*\): flags=.*'`
INET=""
ACTIVE=""
}
function find_links ()
{
IFS=$'\n'
for LINE in `ifconfig`; do
if [[ $LINE != $'\t'* ]]; then new_link "$LINE"; fi
if [[ $LINE =~ "inet " ]]; then INET="$LINE"; fi
if [[ $LINE =~ "status: " ]] && ! [[ $LINE =~ inactive ]]; then ACTIVE="TRUE"; fi
done
unset IFS
new_link "$LINE"
}
function flush ()
{
RETRIES=10
while [ $RETRIES -gt 0 ]; do
if [ $RETRIES -eq 10 ]; then echo -n "Flushing routes."; else echo -n '.'; fi
OUT=`route -n flush 2>&1`
RET=$?
if [[ $RET -eq 0 ]] && [[ -z $OUT ]]; then break; fi
sleep 1
let "RETRIES--"
if [ $RETRIES -eq 0 ]; then
echo " Failed"
exit 1
fi
done
echo " Done"
}
# Main
LINK=""
INET=""
ACTIVE=""
# Verify root
if [ `id -u` -ne 0 ]; then
echo "Must be root"
exit 1
fi
# Flush routes
flush
# Loop over interfaces, reset each
IFS=$'\n'
for LINK in `find_links`; do
echo "Resetting link $LINK"
ifconfig $LINK down
ifconfig $LINK up
done
unset IFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment