Skip to content

Instantly share code, notes, and snippets.

@spence
Last active December 24, 2015 07:39
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 spence/6765000 to your computer and use it in GitHub Desktop.
Save spence/6765000 to your computer and use it in GitHub Desktop.
CRON to resolve and update the IP addresses used throughout the Gasmask *.hst config files. Tested only on OSX 10.8.3 with Gasmask 0.7.
#!/bin/bash
# Job for automatically resolving and updating GasMask host file entries.
#
# Spencer Creasey (github.com/spence)
#
# Gasmask: http://www.clockwise.ee/gasmask/
#
# To use, on the line before your host entry, add:
#
# # fetchip <resolvable hostname>
#
# Example:
#
# If host entry contains:
#
# # fetchip dev.site.com
# 127.0.0.1 site.com
#
# And "dev.site.com" resoves to 10.10.10.10, running this will update the host file to:
#
# # fetchip dev.site.com (updated 1970-01-01T00:00:00)
# 10.10.10.10 site.com
#
# Running again will update the datetime.
#
# Features:
#
# - Supports multiple unique entires of "# fetchip" per file.
# - Ignores entries without "# fetchip".
#
# To install:
#
# 1. Place file in CRON folder (e.g., ~/.cron/fetchip-gasmask.hosts.sh)
# 2. Add execute permissions
# > chmod +x ~/.cron/fetchip-gasmask.hosts.sh
# 3. Update crontab
# > crontab -e
# */5 * * * * /Users/myusername/.cron/fetchip-gasmask-hosts.sh
date=$(date +%Y-%m-%dT%H:%M:%S)
for file in /Users/$USER/Library/Gas\ Mask/Local/*.hst /etc/hosts; do
hosts=$(sed -E -n 's/^# *fetchip +([^ ]+)( +\(updated [-T0-9 :]+\))?/\1/p' "$file") || continue
for host in $hosts; do
ip=$(dig +short $host | grep -e '^\d' | head -n 1)
escaped_host=$(echo "$host" | sed 's/[^-A-Za-z0-9_]/\\&/g')
if [[ -n $ip ]]; then
sed -E -i.bak "/^# *fetchip +$escaped_host/ { s/^(# *fetchip +[^ ]+)( +\(updated [-T0-9 :]+\))?/\1 (updated $date)/; n ; s/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} +(.*)/$ip \1/; }" "$file"
rm "$file.bak"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment