Created
April 7, 2012 12:47
-
-
Save shello/2328579 to your computer and use it in GitHub Desktop.
Logs your public IP address and respective hostname into a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Public IP logger r2 (2012/04/10) | |
# Filipe Rodrigues <shello@shello.org> | |
# Logs your public IP address into a file | |
LC_TIME=C # Deactivate for the system time locale | |
LOGFILE="${HOME}/publicip/publicip.log" # Logfile | |
CURR_DATE=`date` | |
# IP address lookup using OpenDNS | |
CURR_IP=`dig @208.67.222.220 +short myip.opendns.com` | |
if [[ $? != 0 ]]; then | |
exit 1 | |
fi | |
# Reverse lookup, to get the hostname | |
CURR_HOST=`dig -x ${CURR_IP} +short` | |
# Format the log line to have the date and IP address and hostname separated by | |
# a tab character; don't print the hostname if it has no useful information | |
LOGLINE="$(printf "${CURR_DATE}\t${CURR_IP}")" | |
if [[ "${CURR_HOST}" != "${CURR_IP}" ]]; then | |
LOGLINE="$(printf "${LOGLINE}\t${CURR_HOST}")" | |
fi | |
# Update the access time for the log file, and create it if it's missing; | |
touch -a ${LOGFILE} | |
# If the log isn't zero-size, check the last IP address | |
if [[ -s "${LOGFILE}" ]]; then | |
LAST_IP=`tail -n 1 "${LOGFILE}" | cut -f 2` | |
if [[ "${LAST_IP}" == "${CURR_IP}" ]]; then | |
exit 0 | |
fi | |
fi | |
printf "${LOGLINE}\n" >> ${LOGFILE} | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment