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.

131 lines
4.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/bin/env bash
  2. # WAN IP Checker - Whenever server WAN IP address changes, inform admins via email
  3. # Copyright (C) 2018 Pekka Helenius
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ###########################################################
  18. # A script for remote server environments which are behind
  19. # dynamic (non-static) DHCP. Usually these dynamic IPs are
  20. # used in common household networks in non-corporate
  21. # environments.
  22. ###########################################################
  23. # Script requirements
  24. #
  25. # SSMTP
  26. # https://wiki.archlinux.org/index.php/SSMTP
  27. # Relevant conf files
  28. # /etc/ssmtp/revaliases
  29. # /etc/ssmtp/ssmtp.conf
  30. # Because your email password is stored as cleartext in /etc/ssmtp/ssmtp.conf, it is important that this file
  31. # is secure. By default, the entire /etc/ssmtp directory is accessible only by root and the mail group.
  32. # The /usr/bin/ssmtp binary runs as the mail group and can read this file. There is no reason to add
  33. # yourself or other users to the mail group.
  34. ###########################################################
  35. # Some lines below are commented out because the timer is handled by systemd service file
  36. # If you don't use provided systemd service file, re-enable the relevant lines below
  37. function checkWANIP {
  38. # Command to resolve the current IPv4 WAN address
  39. local WANIP_CURRENT="dig +short myip.opendns.com @resolver1.opendns.com"
  40. # Log file timestamp format
  41. local TIMESTAMP=$(date '+%d-%m-%Y, %X')
  42. ############################
  43. # Email sender
  44. local EMAIL_SENDER="mailsender@foo.com"
  45. # Emails to send notification to
  46. local EMAIL_RECIPIENTS=(
  47. "whogetsthemail_1@foo.com"
  48. "whogetsthemail_2@bar.com"
  49. )
  50. ############################
  51. # Email send function
  52. function mailSend {
  53. echo -e "To: ${1}\nFrom: ${EMAIL_SENDER}\nSubject: ${SUBJECT_EMAIL}\n\n${MESSAGE_EMAIL}" | sendmail -v "${1}"
  54. }
  55. ############################
  56. # If we are connected to internet...
  57. # There's no point to do WAN IP check if we can't establish connection to WAN/Internet at all
  58. # In addition, do not generate any network related variables if the connection
  59. # can't be established. Therefore, include variable defitions inside this if statement.
  60. if [[ $(printf $(eval "${WANIP_CURRENT}" &> /dev/null)$?) -eq 0 ]]; then
  61. ############################
  62. # Cache/Log directory of the script
  63. local WANIP_DIR="$HOME"
  64. # Log file for checked/resolved IPv4 WAN addresses
  65. local WANIP_LOG="$WANIP_DIR/.ip_wan.log"
  66. if [[ ! -d "${WANIP_DIR}" ]]; then
  67. mkdir -p "${WANIP_DIR}"
  68. fi
  69. if [[ ! -f "${WANIP_LOG}" ]]; then
  70. printf 'Time\t\t\t\tWAN IPv4\n' > "${WANIP_LOG}"
  71. fi
  72. # Email subject/title
  73. local SUBJECT_EMAIL="WAN IP address changed (Helsinki, $(tail -1 ${WANIP_LOG} | awk '{print $NF}') -> $(eval ${WANIP_CURRENT}))"
  74. # Email message/body contents
  75. local MESSAGE_EMAIL="${TIMESTAMP}: WAN address of location (Helsinki) has been changed from $(tail -1 ${WANIP_LOG} | awk '{print $NF}') to $(eval ${WANIP_CURRENT})"
  76. # Message to server stdout
  77. 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})"
  78. ############################
  79. # Log write command
  80. local LOG_WRITE=$(printf '%s %s\t\t%s\n' $(echo "${TIMESTAMP}") $(eval "${WANIP_CURRENT}") >> "${WANIP_LOG}")
  81. ############################
  82. if [[ $(tail -1 "${WANIP_LOG}" | awk '{print $NF}') != $(printf '%s' $(eval "${WANIP_CURRENT}")) ]] || \
  83. [[ $(cat "${WANIP_LOG}" | wc -l) -le 2 ]] ; then
  84. echo -e "${MESSAGE_STDOUT}"
  85. for i in "${EMAIL_RECIPIENTS[@]}"; do
  86. mailSend "${i}"
  87. $LOG_WRITE
  88. done
  89. fi
  90. fi
  91. }
  92. ############################
  93. checkWANIP