WAN IPv4 checker & email notifier for computers behind dynamic IP/DHCP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.2 KiB

6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
4 years ago
  1. # WAN IP checker
  2. WAN IPv4 checker & email notifier for computer environments behind dynamic IP address.
  3. ## About
  4. In many home networks, WAN (Wide Area Network) IP addresses are dynamically allocated by a local ISP. Usually this is acceptable in common household/home use, but not in server use where static DHCP lease/static IP address is very much a mandatory requirement.
  5. If configuring a static WAN IP is not an option to you, consider setting up a simple notifier which let's you know when your WAN IP address changes.
  6. Static IPs are usually offered only to corporate environments, and not everyone wants to pay extra for such in order to establish a simple server environment in home.
  7. This repository contains a Systemd service & a simple bash script to refresh current WAN IPv4 of a Linux computer, and notify administration for any changes in the WAN IPv4 (Internet) address. This method addresses to several issues:
  8. - Administration is always aware of the current server IPv4 address, whether the address is dynamic or not.
  9. - Administration is able to remotely connect to the server environment although the server IP has been changed. This is possible because admins are notified about any WAN IPv4 address changes via email by the server itself, automatically.
  10. ## Failure countermeasures
  11. The WAN IP checker script has several countermeasures for the following failures:
  12. - **DNS name resolution fails**: DNS servers defined in `/etc/resolv.conf` can not be contacted
  13. - Fall back to DNS servers listed in [wanchecker.sh](ssmtp_conf-sample/wanchecker.sh)
  14. - **Sending email fails**
  15. - Attempt several times until informing that sending an email message has failed
  16. ------------
  17. ## Requirements
  18. - Linux OS
  19. - systemd
  20. - [sSMTP](https://wiki.archlinux.org/index.php/SSMTP) - (SMTP) email client (package: `ssmtp` (Arch Linux), `ssmtp` (Ubuntu))
  21. - curl
  22. - Recommended curl build options for proper DNS fallback support: `--enable-dnsshuffle` and `--enable-ares` (requires `c-ares` package)
  23. - If you work on IPv4-only environments, consider building curl with `--disable-ipv6` option
  24. - bash
  25. - awk
  26. - cat
  27. - eval
  28. - grep
  29. - sed
  30. - wc
  31. - **Optionally:** [resolvconf-override (GitHub/hadess)](https://github.com/hadess/resolvconf-override)
  32. ## Contents
  33. - systemd **system** service file: [wanchecker.service](wanchecker.service)
  34. - Service is run as user `mail` and group `mail`. Change these if necessary.
  35. - systemd **system** timer file: [wanchecker.timer](wanchecker.timer)
  36. - WAN IP check interval is customizable in systemd timer file. Default value is `15min`
  37. - [sSMTP sample configuration files](ssmtp_conf-sample)
  38. - [ssmtp.conf](ssmtp_conf-sample/ssmtp.conf)
  39. - [revaliases](ssmtp_conf-sample/revaliases)
  40. - [wanchecker.sh](ssmtp_conf-sample/wanchecker.sh)
  41. - [wanchecker.conf](ssmtp_conf-sample/wanchecker.conf)
  42. - Default log file location `/var/spool/mail/ip_wan.log` is defined in this file. The log file is updated only when WAN IPv4 changes have been detected.
  43. ## Manual installation & configuration
  44. **1)** Install `ssmtp` package
  45. **2)** Deploy provided files as follows:
  46. | Repository location | Filesystem location |
  47. |-----------------------------------|----------------------------------------|
  48. | ssmtp_conf-sample/wanchecker.sh | /etc/ssmtp/wanchecker.sh |
  49. | ssmtp_conf-sample/wanchecker.conf | /etc/ssmtp/wanchecker.conf |
  50. | wanchecker.service | /etc/systemd/system/wanchecker.service |
  51. | wanchecker.timer | /etc/systemd/system/wanchecker.service |
  52. | File | Owner (u.g) | Permission bits |
  53. |----------------------------------------|-------------|-----------------|
  54. | /etc/ssmtp/wanchecker.sh | root.mail | 0750 |
  55. | /etc/ssmtp/wanchecker.conf | root.mail | 0640 |
  56. | /etc/systemd/system/wanchecker.service | root.root | 0644 |
  57. | /etc/systemd/system/wanchecker.service | root.root | 0644 |
  58. **3)** Configure `sSMTP` files `/etc/ssmtp/revaliases` ([sample](ssmtp_conf-sample/revaliases)), `/etc/ssmtp/ssmtp.conf` ([sample](ssmtp_conf-sample/ssmtp.conf)). More information about these files on [sSMTP Arch Wiki site](https://wiki.archlinux.org/index.php/SSMTP).
  59. **4)** Configure file `/etc/ssmtp/wanchecker.conf` ([sample](ssmtp_conf-sample/wanchecker.conf))
  60. **5)** If necessary, configure sSMTP as described in [sSMTP Readme file](ssmtp_conf-sample/README.md).
  61. **6)** Run (as root or with `sudo`):
  62. ```
  63. systemctl enable wanchecker.timer && \
  64. systemctl start wanchecker.timer
  65. ```
  66. **7) (optional)** Install [resolvconf-override](https://github.com/hadess/resolvconf-override)
  67. ## Arch Linux installation & configuration
  68. **1)** Use provided [PKGBUILD](arch_linux/PKGBUILD) file
  69. **2)** Configure files `/etc/ssmtp/revaliases` ([sample](ssmtp_conf-sample/revaliases)) and `/etc/ssmtp/ssmtp.conf` ([sample](ssmtp_conf-sample/ssmtp.conf)). More information about these files on [sSMTP Arch Wiki site](https://wiki.archlinux.org/index.php/SSMTP).
  70. **3)** Configure file `/etc/ssmtp/wanchecker.conf` ([sample](ssmtp_conf-sample/wanchecker.conf))
  71. **4)** Run (as root or with `sudo`):
  72. ```
  73. systemctl enable wanchecker.timer && \
  74. systemctl start wanchecker.timer
  75. ```
  76. **5) (optional)** Install [resolvconf-override](https://github.com/hadess/resolvconf-override). Use provided Arch Linux [PKGBUILD file](arch_linux/resolvconf-override/PKGBUILD).
  77. ----------
  78. ## Images
  79. When server computer discovers a change in its WAN IPv4, it automatically sends an email notification to system administrator(s):
  80. ![](images/wanip_email.png)
  81. Additionally, server computer keeps a log file which include WAN IPv4 changes and corresponding timestamps:
  82. ![](images/wanip_log.png)
  83. ## Useful commands
  84. - `systemctl --all list-timers` = list all system timers on Linux system, including `wanchecker` timer
  85. - `systemctl is-active wanchecker.timer` = tells whether `wanchecker` timer is running or not
  86. - `systemctl status wanchecker.timer` = more compherensive output about the status of `wanchecker` timer
  87. ## License
  88. This repository uses GPLv3 license. See [LICENSE](./LICENSE) file for details.