I run this little script to notify me when the IP changes.  However at time to time the 
file: "old_ip.txt"
is being populated with an empty line "no IP address"

What could be causing it?


#!/bin/bash

# Replace "YOUR_EMAIL" with your actual email address
EMAIL="your_em...@gmail.com"

# File to store the old IP address
IP_FILE="/home/fd/business/scripts/old_ip.txt"

# Read the old IP address from the file
OLD_IP=$(cat "$IP_FILE")

# Function to retrieve the IP address
get_ip_address() {
    local ip_address
    ip_address=$(curl -s https://ifconfig.me/ip)
    echo "$ip_address"
}

# Query the API to get the current IP address
NEW_IP=$(curl -s https://api.ipify.org)

sleep 2

# Compare the new IP address with the old one
if [[ "$NEW_IP" != "$OLD_IP" ]]; then
    echo "Your IP address has changed to $NEW_IP"

    # Send an email notification
    echo "New IP address: $NEW_IP" | mailto -s "IP address change" $EMAIL

    # Allow overwriting of the file
    set +o noclobber

    # Update the old IP address in the file
#     printf "%s" "$NEW_IP" > "$IP_FILE"
     echo -n "$NEW_IP" > "$IP_FILE"

    # Restore the noclobber option
    set -o noclobber

else
    echo "Your IP address is still $OLD_IP"
fi


--
Thelma

Reply via email to