Last active
October 10, 2020 18:53
-
-
Save joemaller/520df6215d67b78f1086f41002a07d48 to your computer and use it in GitHub Desktop.
Script for logging IP address changes. Checked with shfmt: `docker run --rm -it -v "$PWD":/src peterdavehello/shfmt shfmt -i 2 -w /src/ip-address-log.sh`
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 | |
# Stop on errors, unset vars and broken pipes | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
LOG_FILE=${1:-} | |
if [ -z "$LOG_FILE" ]; then | |
echo "Please provide a logfile as the first argument" | |
exit 1 | |
fi | |
CURRENT_IP=$(curl -s ifconfig.me) | |
LOG_IP=$(date "+%Y-%m-%dT%H:%M:%S%z")"\t$CURRENT_IP" | |
if [ ! -f "$1" ]; then | |
# Log file doesn't exist yet, create the file | |
echo "Creating $LOG_FILE and logging IP address..." | |
echo -e $LOG_IP >"$LOG_FILE" | |
else | |
# Log file exists, check last entry | |
PREV_IP=$(tail -n1 $LOG_FILE | cut -f 2) | |
if [ "$PREV_IP" != "$CURRENT_IP" ]; then | |
echo "New IP, logging to $LOG_FILE..." | |
echo -e "$LOG_IP" >>"$LOG_FILE" | |
else | |
echo "IP Address is unchanged" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment