The Best Free Tools to Email My IP Instantly

Written by

in

Automate Your Network: Setup an ‘Email My IP’ Script Managing a home server, Raspberry Pi, or remote workstation often requires knowing its public IP address. However, most residential internet service providers assign dynamic IP addresses that change unexpectedly. Instead of paying for a static IP or setting up complex dynamic DNS software, you can build a lightweight automation script. This guide will show you how to write a Python script that detects your public IP address and emails it to you automatically whenever it changes. Prerequisites

Before writing the script, ensure you have the following components ready: Python 3: Installed on your target machine.

App Password: A secure, 16-digit password generated from your email provider (e.g., Gmail, Outlook) to allow script access without using your primary password.

Linux Environment: Access to the terminal and cron for scheduling (though Windows Task Scheduler works similarly). Step 1: Create the Python Script

This script fetches your current public IP address using an external API, compares it to the last known IP address stored in a local file, and sends an email notification if a change is detected.

Create a file named email_ip.py and paste the following code:

import urllib.request import smtplib import os from email.mime.text import MIMEText # — CONFIGURATION — SMTP_SERVER = “://gmail.com” SMTP_PORT = 465 SENDER_EMAIL = “[email protected]” SENDER_PASSWORD = “your_app_password” RECIPIENT_EMAIL = “[email protected]” IP_LOG_FILE = os.path.join(os.path.dirname(file), “current_ip.txt”) # ——————— def get_public_ip(): try: with urllib.request.urlopen(”https://ipify.org”) as response: return response.read().decode(“utf-8”).strip() except Exception as e: print(f”Error fetching IP: {e}“) return None def read_last_ip(): if os.path.exists(IP_LOG_FILE): with open(IP_LOG_FILE, “r”) as f: return f.read().strip() return “” def write_current_ip(ip): with open(IP_LOG_FILE, “w”) as f: f.write(ip) def send_email(current_ip): msg = MIMEText(f”Your remote machine’s public IP address has changed. New IP: {current_ip}“) msg[“Subject”] = f”⚠️ IP Address Update: {current_ip}” msg[“From”] = SENDER_EMAIL msg[“To”] = RECIPIENT_EMAIL try: with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server: server.login(SENDER_EMAIL, SENDER_PASSWORD) server.sendmail(SENDER_EMAIL, [RECIPIENT_EMAIL], msg.as_string()) print(“Notification email sent successfully.”) except Exception as e: print(f”Failed to send email: {e}“) def main(): current_ip = get_public_ip() if not current_ip: return last_ip = read_last_ip() if current_ip != last_ip: print(f”IP change detected! Old: {last_ip} -> New: {current_ip}“) send_email(current_ip) write_current_ip(current_ip) else: print(“IP address has not changed.”) if name == “main”: main() Use code with caution. Step 2: Test the Script

Run the script manually in your terminal to verify that it functions correctly: python3 email_ip.py Use code with caution.

First Run: The script creates current_ip.txt, detects your IP, and sends an email.

Second Run: The output should state “IP address has not changed,” and no email will be sent.

Troubleshooting: If the script fails, verify your App Password and ensure your email provider allows standard SMTP traffic. Step 3: Automate with Cron

To make this network utility useful, configure it to run automatically at regular intervals using the Linux cron utility. Open the crontab configuration tool: crontab -e Use code with caution.

Add a new line at the bottom of the file to execute the script every hour. Replace the path with the absolute path to your script:

0/usr/bin/python3 /absolute/path/to/email_ip.py > /dev/null 2>&1 Use code with caution. Save and close the editor. Conclusion

You now have a lightweight, self-hosted solution monitoring your network topology. This script keeps you informed of IP address changes without relying on third-party dynamic DNS services or introducing bloated background software to your system. To tailor this setup to your network, let me know: What operating system is your server running?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *