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.

163 lines
6.5 KiB

5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 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. **NOTE:** I currently use proper Linux network namespace setup for this script and for fallback DNS servers. Therefore, in my point of view, introduced `LD_PRELOAD` method is obsolete and I do not recommend using it anymore (I actually found it quite buggy, too). I leave it for legacy purposes if someone finds it useful, anyway.
  4. ## About
  5. 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.
  6. 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.
  7. 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.
  8. 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:
  9. - Administration is always aware of the current server IPv4 address, whether the address is dynamic or not.
  10. - 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.
  11. ## Failure countermeasures
  12. The WAN IP checker script has several countermeasures for the following failures:
  13. - **DNS name resolution fails**: DNS servers defined in `/etc/resolv.conf` can not be contacted
  14. - Fall back to DNS servers listed in [wanchecker.sh](ssmtp_conf-sample/wanchecker.sh)
  15. - **Sending email fails**
  16. - Attempt several times until informing that sending an email message has failed
  17. ------------
  18. ## Requirements
  19. - Linux OS
  20. - systemd
  21. - [sSMTP](https://wiki.archlinux.org/index.php/SSMTP) - (SMTP) email client (package: `ssmtp` (Arch Linux), `ssmtp` (Ubuntu))
  22. - curl
  23. - Recommended curl build options for proper DNS fallback support: `--enable-dnsshuffle` and `--enable-ares` (requires `c-ares` package)
  24. - If you work on IPv4-only environments, consider building curl with `--disable-ipv6` option
  25. - bash
  26. - awk
  27. - cat
  28. - eval
  29. - grep
  30. - sed
  31. - wc
  32. - **Optionally:** [resolvconf-override (GitHub/hadess)](https://github.com/hadess/resolvconf-override)
  33. ## Contents
  34. - systemd **system** service file: [wanchecker.service](wanchecker.service)
  35. - Service is run as user `mail` and group `mail`. Change these if necessary.
  36. - systemd **system** timer file: [wanchecker.timer](wanchecker.timer)
  37. - WAN IP check interval is customizable in systemd timer file. Default value is `15min`
  38. - [sSMTP sample configuration files](ssmtp_conf-sample)
  39. - [ssmtp.conf](ssmtp_conf-sample/ssmtp.conf)
  40. - [revaliases](ssmtp_conf-sample/revaliases)
  41. - [wanchecker.sh](ssmtp_conf-sample/wanchecker.sh)
  42. - [wanchecker.conf](ssmtp_conf-sample/wanchecker.conf)
  43. - 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.
  44. ## Manual installation & configuration
  45. **1)** Install `ssmtp` package
  46. **2)** Deploy provided files as follows:
  47. | Repository location | Filesystem location |
  48. |-----------------------------------|----------------------------------------|
  49. | ssmtp_conf-sample/wanchecker.sh | /etc/ssmtp/wanchecker.sh |
  50. | ssmtp_conf-sample/wanchecker.conf | /etc/ssmtp/wanchecker.conf |
  51. | wanchecker.service | /etc/systemd/system/wanchecker.service |
  52. | wanchecker.timer | /etc/systemd/system/wanchecker.service |
  53. | File | Owner (u.g) | Permission bits |
  54. |----------------------------------------|-------------|-----------------|
  55. | /etc/ssmtp/wanchecker.sh | root.mail | 0750 |
  56. | /etc/ssmtp/wanchecker.conf | root.mail | 0640 |
  57. | /etc/systemd/system/wanchecker.service | root.root | 0644 |
  58. | /etc/systemd/system/wanchecker.service | root.root | 0644 |
  59. **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).
  60. **4)** Configure file `/etc/ssmtp/wanchecker.conf` ([sample](ssmtp_conf-sample/wanchecker.conf))
  61. **5)** If necessary, configure sSMTP as described in [sSMTP Readme file](ssmtp_conf-sample/README.md).
  62. **6)** Run (as root or with `sudo`):
  63. ```
  64. systemctl enable wanchecker.timer && \
  65. systemctl start wanchecker.timer
  66. ```
  67. **7) (optional)** Install [resolvconf-override](https://github.com/hadess/resolvconf-override)
  68. ## Arch Linux installation & configuration
  69. **1)** Use provided [PKGBUILD](arch_linux/PKGBUILD) file
  70. **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).
  71. **3)** Configure file `/etc/ssmtp/wanchecker.conf` ([sample](ssmtp_conf-sample/wanchecker.conf))
  72. **4)** Run (as root or with `sudo`):
  73. ```
  74. systemctl enable wanchecker.timer && \
  75. systemctl start wanchecker.timer
  76. ```
  77. **5) (optional)** Install [resolvconf-override](https://github.com/hadess/resolvconf-override). Use provided Arch Linux [PKGBUILD file](arch_linux/resolvconf-override/PKGBUILD).
  78. ----------
  79. ## Images
  80. When server computer discovers a change in its WAN IPv4, it automatically sends an email notification to system administrator(s):
  81. ![](images/wanip_email.png)
  82. Additionally, server computer keeps a log file which include WAN IPv4 changes and corresponding timestamps:
  83. ![](images/wanip_log.png)
  84. ## Useful commands
  85. - `systemctl --all list-timers` = list all system timers on Linux system, including `wanchecker` timer
  86. - `systemctl is-active wanchecker.timer` = tells whether `wanchecker` timer is running or not
  87. - `systemctl status wanchecker.timer` = more compherensive output about the status of `wanchecker` timer
  88. ## License
  89. This repository uses GPLv3 license. See [LICENSE](./LICENSE) file for details.