Skip to content

Instantly share code, notes, and snippets.

@3isenHeiM
Last active February 8, 2024 10:03
Show Gist options
  • Save 3isenHeiM/fbced88bace1d9fb110adb0c2509c5d8 to your computer and use it in GitHub Desktop.
Save 3isenHeiM/fbced88bace1d9fb110adb0c2509c5d8 to your computer and use it in GitHub Desktop.
Track Public IP for OPNsense
#!/bin/sh
TRACKING_FILE="/tmp/public_ip.log"
# Get Public IP adress
get_public_ip() {
# dig @resolver4.opendns.com myip.opendns.com +short -4
# OPNsense does not have dig
drill @resolver4.opendns.com myip.opendns.com | grep "myip.opendns" | grep -v ";" | awk '{print $5}'
}
update_file() {
timestamp=$(date +"%Y-%m-%d %T")
# Log the IP address and timestamp to the file
echo "$1 since $timestamp" >> $TRACKING_FILE
}
# Initialize the previous IP address
if [ -f "$TRACKING_FILE" ]; then
prev_ip=$(tail -n 1 "$TRACKING_FILE" | awk '{print $1}')
else
prev_ip=$(get_public_ip)
# Initialize the file
update_file "$prev_ip"
fi
# Get the current IP address
current_ip=$(get_public_ip)
# Check if the IP address has changed
if [ "$current_ip" != "$prev_ip" ]; then
# Write new IP to file
update_file "$current_ip"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment