Skip to content

Instantly share code, notes, and snippets.

@kastiglione
Created July 4, 2011 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kastiglione/1062860 to your computer and use it in GitHub Desktop.
Save kastiglione/1062860 to your computer and use it in GitHub Desktop.
Hostname banning zsh function
# assumes control of all hosts in /Local/Default/Hosts
# https://gist.github.com/1062860
hostoggle() {
if (( ${#*} == 0 )); then
# no host given, print current list of hosts banned
dscl . -ls Hosts
fi
for host in "$@"; do
if $(dscl . -read Hosts/$host >&- 2>&-); then
# delete existing host records (raw and www-less)
sudo dscl . -delete Hosts/$host
[[ "$host" =~ "^www\." ]] && sudo dscl . -delete "Hosts/${host#www.}" >&- 2>&-
else
# create host record misdirection (raw and www-less)
sudo dscl . -create Hosts/$host IPAddress 127.0.0.1
[[ "$host" =~ "^www\." ]] && sudo dscl . -create "Hosts/${host#www.}" IPaddress 127.0.0.1
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment