Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IP Notifier

Monitor your Ubuntu server's IP address and receive email notifications when it changes.

Features

  • 🔍 Monitors private IP address at configurable intervals
  • 📧 Sends email notifications when IP changes
  • 📝 Comprehensive logging
  • ⚙️ Easy configuration via environment variables
  • 🚀 Uses UV for fast and reliable package management

Requirements

  • Python 3.11 or higher
  • UV package manager
  • SMTP email account (Gmail, etc.)

Installation

1. Install UV (if not already installed)

curl -LsSf https://astral.sh/uv/install.sh | sh

2. Clone this repository

cd /path/to/ip-notifier

3. Install dependencies

uv sync

4. Configure email settings

Copy the example environment file and edit it:

cp .env.example .env
nano .env

Update the following variables:

SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASSWORD=your-app-password
EMAIL_FROM=your-email@gmail.com
EMAIL_TO=recipient@example.com
CHECK_INTERVAL=300  # Check every 5 minutes

For Gmail users:

  • You need to use an App Password instead of your regular password
  • Enable 2-factor authentication first, then generate an app password

Usage

Run the IP monitor

uv run ip_notifier

Run in background

You can run it in the background using nohup:

nohup uv run ip-notifier > /dev/null 2>&1 &

Or use screen or tmux for a persistent session:

# Using screen
screen -S ip-notifier
uv run ip-notifier
# Press Ctrl+A then D to detach

# Using tmux
tmux new -s ip-notifier
uv run ip-notifier
# Press Ctrl+B then D to detach

Stop the monitor

If running in foreground, press Ctrl+C.

If running in background, find and kill the process:

ps aux | grep ip-notifier
kill <PID>

Configuration Options

All configuration is done via environment variables in .env:

Variable Description Default
SMTP_SERVER SMTP server hostname smtp.gmail.com
SMTP_PORT SMTP server port 587
SMTP_USER SMTP username Required
SMTP_PASSWORD SMTP password Required
EMAIL_FROM Sender email address Same as SMTP_USER
EMAIL_TO Recipient email address Required
CHECK_INTERVAL Seconds between checks 300 (5 minutes)
IP_FILE Path to store last IP last_known_ip.txt

Email Notifications

You'll receive email notifications in two cases:

  1. Initial Notification: When the service first starts, you'll get an email with the current IP address
  2. Change Notification: Whenever your IP address changes, you'll get an email with both the old and new IP addresses

Logs

The application creates a log file ip-notifier.log in the current directory. You can view it:

tail -f ip-notifier.log

Auto-start on System Reboot

To run this script automatically after system reboot, you have several options:

Option 1: Add to crontab

crontab -e

Add this line:

@reboot cd /path/to/ip-notifier && /home/your-user/.cargo/bin/uv run ip-notifier >> /tmp/ip-notifier.log 2>&1

Option 2: Add to rc.local (if available)

sudo nano /etc/rc.local

Add before exit 0:

cd /path/to/ip-notifier && /usr/local/bin/uv run ip-notifier &

Option 3: Create a systemd service (recommended)

Create /etc/systemd/system/ip-notifier.service:

[Unit]
Description=IP Address Monitor
After=network-online.target

[Service]
Type=simple
User=your-user
WorkingDirectory=/path/to/ip-notifier
EnvironmentFile=/path/to/ip-notifier/.env
ExecStart=/home/your-user/.cargo/bin/uv run ip-notifier
Restart=always

[Install]
WantedBy=multi-user.target

Then enable and start:

sudo systemctl enable ip-notifier
sudo systemctl start ip-notifier

Troubleshooting

Email not sending

  • Verify SMTP credentials in .env
  • For Gmail, ensure you're using an App Password
  • Check that your server can reach the SMTP server (firewall rules)

IP detection not working

  • Check your internet connection
  • Verify the log file for error messages
  • The script tries multiple IP detection services for reliability

License

MIT License - Feel free to modify and distribute as needed.

About

Monitor server IP address and send email notifications when it changes

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages