Check for IP Address changes on my Raspberry Pi
Script that is called from Cron
cat /home/pi/send_ip.sh
#!/bin/bash
echo -n "$(date '+%Y-%m-%d %H:%M') "
hostname -I | awk '{for(i=1;i<=NF;i++) if ($i ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) printf "%s ", $i; print ""}'
Run this in Cron
$ crontab -e
# +---------------- minute (0 - 59) # | +------------- hour (0 - 23) # | | +---------- day of month (1 - 31) # | | | +------- month (1 - 12) # | | | | +---- day of week (0 - 6) (Sunday=0) # | | | | | # * * * * * command to be executed # 0 8 * * * /opt/scripts/down.sh >> /home/pi/log/shade.log 2>&1 # 0 11 * * * /opt/scripts/up.sh >> /home/pi/log/shade.log 2>&1 1 7 * * * /home/pi/send_ip.sh | ssh shade@server "cat >> /home/shade/log/shade.log" 2>> /home/pi/cron_debug.log
Show the file on the server that logs the IP address each day
ls -l /home/shade/log total 4 -rw-rw-rw- 1 shade shade 2519 Sep 19 07:40 shade.log
Show the contents of the shade.log file
$ cat shade.log # log file for host IP of shade Raspberry Pi Started out as 192.168.1.118 Jun 9, 2025 2025-06-09 20:01 192.168.1.118 2025-06-10 07:01 192.168.1.118 2025-06-11 07:01 192.168.1.118
Get Internal and External IP and date and time
#!/bin/bash
# 1. Get Public IP (with a break so it doesn't overwrite a success)
SERVICES=("https://ifconfig.me" "https://api.ipify.org" "https://icanhazip.com")
Ext_IP="Public IP: Not Found"
for url in "${SERVICES[@]}"; do
PUBLIC_IP=$(curl -s --max-time 5 "$url")
if [ -n "$PUBLIC_IP" ]; then
Ext_IP="Public IP: $PUBLIC_IP"
break # Important: Stop looking once we find one!
fi
done
# 2. Get Internal IP and strip the trailing newline
# We use 'tr -d "\n"' to ensure it stays on the same line
INT_IP=$(hostname -I | awk '{for(i=1;i<=NF;i++) if ($i ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) printf "%s ", $i}' | tr -d '\n')
# 3. Output everything at once
echo "$(date '+%Y-%m-%d %H:%M') | Int_IP: $INT_IP | $Ext_IP"
