Browse Source

Initial commit

master
Fincer 6 years ago
commit
b4fdbf02f0
5 changed files with 233 additions and 0 deletions
  1. +68
    -0
      README.md
  2. BIN
      images/wanip_email.png
  3. BIN
      images/wanip_log.png
  4. +140
    -0
      wanip-checker.sh
  5. +25
    -0
      wanip-checker@.service

+ 68
- 0
README.md View File

@ -0,0 +1,68 @@
# WAN IP checker
WAN IPv4 checker & email notifier for server environments behind dynamic DHCP.
## About
This repository contains a systemd service file & a simple bash script to refresh current WAN IPv4 of a server, and notify server admin for any changes in the server WAN (Internet) address. This helps in several issues:
- Server admin is always aware of the current server IPv4 address, whether the address is dynamic or not.
- Server admin is able to remotely connect to the server environment although the server IP may have been changed. This is possible because the admin is notified about any WAN IPv4 address changes via email by the server itself, automatically.
In many home networks, WAN (Wide Area Network) IP addresses are dynamically allocated by a local ISP. Usually this is okay in common household/home use, but not in server use.
In most server environments, static DHCP lease/static IP address is a mandatory requirement. However, static IPs are usually offered only for corporate environments, and not everyone wants to pay extra for such in order to establish a simple server environment in home.
## Requirements
- A server computer of any kind
- Linux OS
- systemd - service file
- [SSMTP](https://wiki.archlinux.org/index.php/SSMTP) - email client
- bash
- awk
## Contents
- systemd **user** service file: `wanip-checker@.service`
- bash script: `wanip-checker.sh`
## Installation
**1)** Insert `wanip-checker@.service` into `/usr/lib/systemd/user/` folder
- WAN IP check interval is customizable in systemd service file. Default value is `60` (1 min)
**2)** Insert `wanip-checker.sh` into your `/home/myuser/` folder
**3)** Configure your email address and message form in `wanip-checker.sh` file
**3)** Install `ssmtp`, and configure files `/etc/ssmtp/revaliases` and `/etc/ssmtp/ssmtp.conf` as described on [SSMTP Arch Wiki site](https://wiki.archlinux.org/index.php/SSMTP).
**4)** Run
```
systemctl --user enable wanip-checker@myusername.service && \
systemctl --user start wanip-checker@myusername.service && \
systemctl --user daemon-reload
```
**NOTE:** If you change the script contents, make sure to run `systemctl --user restart wanip-checker@myusername.service` afterwards.
## Images
When server computer discovers changes in WAN IPv4, it automatically sends an email notification for system administrators:
![](images/wanip_email.png)
Additionally, server computer keeps a log file which include WAN IPv4 changes and corresponding timestamps:
![](images/wanip_log.png)

BIN
images/wanip_email.png View File

Before After
Width: 827  |  Height: 127  |  Size: 7.2 KiB

BIN
images/wanip_log.png View File

Before After
Width: 645  |  Height: 78  |  Size: 4.1 KiB

+ 140
- 0
wanip-checker.sh View File

@ -0,0 +1,140 @@
#!/bin/env bash
# WAN IP Checker - Whenever server WAN IP address changes, inform admins via email
# Copyright (C) 2018 Pekka Helenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###########################################################
# A script for remote server environments which are behind
# dynamic (non-static) DHCP. Usually these dynamic IPs are
# used in common household networks in non-corporate
# environments.
###########################################################
# Script requirements
#
# SSMTP
# https://wiki.archlinux.org/index.php/SSMTP
# Relevant conf files
# /etc/ssmtp/revaliases
# /etc/ssmtp/ssmtp.conf
# Because your email password is stored as cleartext in /etc/ssmtp/ssmtp.conf, it is important that this file
# is secure. By default, the entire /etc/ssmtp directory is accessible only by root and the mail group.
# The /usr/bin/ssmtp binary runs as the mail group and can read this file. There is no reason to add
# yourself or other users to the mail group.
###########################################################
# Some lines below are commented out because the timer is handled by systemd service file
# If you don't use provided systemd service file, re-enable the relevant lines below
function checkWANIP() {
# Command to resolve the current IPv4 WAN address
local WANIP_CURRENT="dig +short myip.opendns.com @resolver1.opendns.com"
############################
# If we are connected to internet...
# There's no point to do WAN IP check if we can't establish connection to WAN/Internet at all
# In addition, do not generate any network related variables if the connection
# can't be established. Therefore, include variable defitions inside this if statement.
if [[ $(printf $(eval ${WANIP_CURRENT} &> /dev/null)$?) -eq 0 ]]; then
# Check interval in minutes
# local CHECK_INTERVAL=5
############################
# Cache/Log directory of the script
local WANIP_DIR="$HOME"
# Log file for checked/resolved IPv4 WAN addresses:
local WANIP_LOG="$WANIP_DIR/.ip_wan.log"
if [[ ! -d ${WANIP_DIR} ]]; then
mkdir -p ${WANIP_DIR}
fi
if [[ ! -f ${WANIP_LOG} ]]; then
printf 'Time\t\t\t\tWAN IPv4\n' > ${WANIP_LOG}
fi
############################
# Log file timestamp format
local TIMESTAMP=$(date '+%d-%m-%Y, %X')
############################
# Email to send notify to
local EMAIL_RECIPIENT="mymail@hotmail.com"
# Email subject/title
local SUBJECT_EMAIL="WAN IP address change (Helsinki, $(tail -1 ${WANIP_LOG} | awk '{print $NF}') -> $(eval ${WANIP_CURRENT}))"
# Email message/body contents
local MESSAGE_EMAIL="${TIMESTAMP}: WAN address of the server (Helsinki) has been changed from $(tail -1 ${WANIP_LOG} | awk '{print $NF}') to $(eval ${WANIP_CURRENT})"
# Message to server stdout
local MESSAGE_STDOUT="$(echo ${TIMESTAMP}) - WAN address of this server has been changed from $(tail -1 ${WANIP_LOG} | awk '{print $NF}') to $(eval ${WANIP_CURRENT})"
############################
# Email send command
local MAIL_SEND="echo -e \"To: ${EMAIL_RECIPIENT}\nFrom: ${EMAIL_RECIPIENT}\nSubject: ${SUBJECT_EMAIL}\n\n${MESSAGE_EMAIL}\" | sendmail -v ${EMAIL_RECIPIENT}"
# Log write command
local LOG_WRITE="printf '%s %s\t\t%s\n' $(echo $TIMESTAMP) $(eval $WANIP_CURRENT) >> $WANIP_LOG"
############################
# If the log file has no previous IPv4 entries
if [[ $(cat $WANIP_LOG | wc -l) -le 1 ]]; then
eval ${LOG_WRITE}
fi
# local i=0
# while true; do
# if [[ $i -ne 0 ]]; then
# sleep $(( ${CHECK_INTERVAL} * 60 ))
# fi
# if [[ -f $WANIP_LOG ]]; then
# The log file must include more than just the header line
if [[ $(cat $WANIP_LOG | wc -l) -gt 1 ]]; then
if [[ $(tail -1 $WANIP_LOG | awk '{print $NF}') != $(printf '%s' $(eval $WANIP_CURRENT)) ]]; then
echo -e ${MESSAGE_STDOUT}
eval ${MAIL_SEND}
eval ${LOG_WRITE}
fi
fi
fi
# let i++
# done
}
############################
checkWANIP

+ 25
- 0
wanip-checker@.service View File

@ -0,0 +1,25 @@
# Should be placed to /usr/lib/systemd/user/
####
# Usage
# systemctl --user enable wanip-checker@<username>.service && \
# systemctl --user start wanip-checker@<username>.service && \
# systemctl --user daemon-reload
[Unit]
Description=WAN IPv4 checker & email sender for dynamic IPv4 server environments
After=network-online.target
[Service]
ExecStart=/usr/bin/bash /home/%i/wanip-checker.sh
# Restart (check) every 1 minute (60 seconds)
RestartSec=60
Restart=always
# Emailing fails if this is enabled
#NoNewPrivileges=true
[Install]
WantedBy=default.target

Loading…
Cancel
Save