Raspberry Pi Public IP Tracker via SCP to AWS¶

Date: 2025-06-23

This guide helps you set up your Raspberry Pi to regularly send its current public IP address to your AWS server. The IP is saved to a file accessible via your personal domain (e.g., https://yourdomain.com/pi-ip.txt).


Prerequisites¶

  • Raspberry Pi connected to the internet.
  • An AWS EC2 instance (Ubuntu recommended) with a web server (like Apache or Nginx) serving /var/www/html/ or another public directory.
  • A domain name pointing to your AWS server (e.g., yourdomain.com).
  • SSH access from your Raspberry Pi to your AWS server using a .pem key.

1. Prepare the SCP Command with Your .pem Key¶

On your Raspberry Pi, you already use a command like this:

scp -i /path/to/your-key.pem filename.html ubuntu@your-ec2-public-dns:/home/ubuntu/website

We will adapt it to send your public IP.


2. Create the IP Update Script on the Pi¶

Create a script named update_ip.sh:

nano ~/update_ip.sh

Paste this content, adjusting your .pem key path and AWS destination folder:

#!/bin/bash

KEY_PATH="/home/pi/update_ip/Janos-Ubuntu-Server-Key-Pair.pem"
REMOTE_USER="ubuntu"
REMOTE_HOST="janosrostas.co.uk"
REMOTE_PATH="/home/ubuntu/website"


LOG_DIR="/home/pi/update_ip"
LAST_IP_FILE="$LOG_DIR/last_ip.txt"
FULL_LOG="$LOG_DIR/pi_ip.txt"

# Get current public IPv4
CURRENT_IP=$(curl -4 -s ifconfig.me)
DATE=$(date '+%Y-%m-%d %H:%M:%S')

# Read last known IP
if [ -f "$LAST_IP_FILE" ]; then
    LAST_IP=$(cat "$LAST_IP_FILE")
else
    LAST_IP=""
fi

# If IP has changed, log it and upload
if [ "$CURRENT_IP" != "$LAST_IP" ]; then
    echo "$CURRENT_IP" > "$LAST_IP_FILE"
    echo "$DATE - IP changed to: $CURRENT_IP" >> "$FULL_LOG"

    # Update to remote location
    scp -i "$KEY_PATH" "$FULL_LOG" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/pi_ip.txt"
fi

Replace:

  • /home/pi/path-to-your-key.pem with the actual path to your .pem key
  • your-ec2-public-dns with your AWS instance's public DNS name or IP address

Make it executable:

chmod +x ~/update_ip.sh

3. Add Cron Job for Regular Updates¶

Edit crontab:

crontab -e

Add this line to update every hour:

0 * * * * /home/pi/update_ip/update_ip.sh

4. Test the Setup¶

Run the script manually first:

~/update_ip.sh

Then visit: https://yourdomain.com/pi-ip.txt to see your Pi’s current public IP.


Your Pi's IP will now always be accessible from your website!


Author¶

János Rostás & ChatGPT
Electronic and Computer Engineering Student | Raspberry Pi Enthusiast | janosrostas.co.uk

No description has been provided for this imageJanosRostas.co.uk