Skip to content

Instantly share code, notes, and snippets.

@vyach-vasiliev
Created December 11, 2020 10:40
Show Gist options
  • Save vyach-vasiliev/a7b980a7ad00694c95d86a8b39daf06d to your computer and use it in GitHub Desktop.
Save vyach-vasiliev/a7b980a7ad00694c95d86a8b39daf06d to your computer and use it in GitHub Desktop.
Flush DNS for Windows, MacOS, Linux
#!/bin/bash
#
# flushdns
# This script will flush your DNS cache on Mac OS X 10.4 - 10.10.
# The method for doing this chages it seems in every version of OS X,
# and I can never remember the correct command, so I wrote this script
# and gave it a name I could remeber.
#
# v.2016-02-01 by [email protected]
OSX_VERSION=`sw_vers -productVersion`
echo "Attempting to flush DNS cache"
case $OSX_VERSION in
# OS X 10.4
10\.4*)
lookupd -flushcache
;;
# OS X 10.5 & 10.6
10\.[56]*)
dscacheutil -flushcache
;;
# OS X 10.7 & 10.8
10\.[78]*)
killall -HUP mDNSResponder
;;
# OS X 10.9
10\.9*)
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
;;
# OS X 10.10 - 10.10.3 - the crappy times
10\.10|10\.10\.[1-3])
sudo discoveryutil mdnsflushcache
sudo discoveryutil udnsflushcache
;;
# OS X 10.10.4 and up -- long live mDBSResponder!
10\.10\.[4-5])
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
;;
# OS X 10.11
10\.11*)
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
;;
# Catch anything else...
*)
echo "Sorry, this script is not prepared to handle your version of OS X."
exit
esac
echo "DNS cache flushed."
exit
#!/bin/bash
echo "Attempting to flush DNS cache"
if [ "$OS" = "Darwin" ]
then
MAJOR_MAC_VERSION=$(sw_vers -productVersion | awk -F '.' '{print $2}')
if [ "$MAJOR_MAC_VERSION" -ge "10" ]
then
sudo killall -HUP mDNSResponder
else
sudo dscacheutil -flushcache
fi
fi
if [ "$OS" = "Linux" ]
then
# Find Unix version and cleares the DNS Cache
MAJOR_UNIX_NAME=$(lsb_release -i | awk -F ":" '{print $2}')
if [ "$MAJOR_UNIX_NAME" = "Ubuntu" ]
then
sudo /etc/rc.d/init.d/nscd restart
fi
fi
echo "DNS cache flushed."
# exit
echo "Attempting to flush DNS cache"
ipconfig /flushdns
nbtstat -r
netsh int ip reset
netsh winsock reset
echo "DNS cache flushed."
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment