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.

140 lines
4.6 KiB

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. ############################
  41. # If we are connected to internet...
  42. # There's no point to do WAN IP check if we can't establish connection to WAN/Internet at all
  43. # In addition, do not generate any network related variables if the connection
  44. # can't be established. Therefore, include variable defitions inside this if statement.
  45. if [[ $(printf $(eval ${WANIP_CURRENT} &> /dev/null)$?) -eq 0 ]]; then
  46. # Check interval in minutes
  47. # local CHECK_INTERVAL=5
  48. ############################
  49. # Cache/Log directory of the script
  50. local WANIP_DIR="$HOME"
  51. # Log file for checked/resolved IPv4 WAN addresses:
  52. local WANIP_LOG="$WANIP_DIR/.ip_wan.log"
  53. if [[ ! -d ${WANIP_DIR} ]]; then
  54. mkdir -p ${WANIP_DIR}
  55. fi
  56. if [[ ! -f ${WANIP_LOG} ]]; then
  57. printf 'Time\t\t\t\tWAN IPv4\n' > ${WANIP_LOG}
  58. fi
  59. ############################
  60. # Log file timestamp format
  61. local TIMESTAMP=$(date '+%d-%m-%Y, %X')
  62. ############################
  63. # Email to send notify to
  64. local EMAIL_RECIPIENT="mymail@hotmail.com"
  65. # Email subject/title
  66. local SUBJECT_EMAIL="WAN IP address change (Helsinki, $(tail -1 ${WANIP_LOG} | awk '{print $NF}') -> $(eval ${WANIP_CURRENT}))"
  67. # Email message/body contents
  68. 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})"
  69. # Message to server stdout
  70. 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})"
  71. ############################
  72. # Email send command
  73. local MAIL_SEND="echo -e \"To: ${EMAIL_RECIPIENT}\nFrom: ${EMAIL_RECIPIENT}\nSubject: ${SUBJECT_EMAIL}\n\n${MESSAGE_EMAIL}\" | sendmail -v ${EMAIL_RECIPIENT}"
  74. # Log write command
  75. local LOG_WRITE="printf '%s %s\t\t%s\n' $(echo $TIMESTAMP) $(eval $WANIP_CURRENT) >> $WANIP_LOG"
  76. ############################
  77. # If the log file has no previous IPv4 entries
  78. if [[ $(cat $WANIP_LOG | wc -l) -le 1 ]]; then
  79. eval ${LOG_WRITE}
  80. fi
  81. # local i=0
  82. # while true; do
  83. # if [[ $i -ne 0 ]]; then
  84. # sleep $(( ${CHECK_INTERVAL} * 60 ))
  85. # fi
  86. # if [[ -f $WANIP_LOG ]]; then
  87. # The log file must include more than just the header line
  88. if [[ $(cat $WANIP_LOG | wc -l) -gt 1 ]]; then
  89. if [[ $(tail -1 $WANIP_LOG | awk '{print $NF}') != $(printf '%s' $(eval $WANIP_CURRENT)) ]]; then
  90. echo -e ${MESSAGE_STDOUT}
  91. eval ${MAIL_SEND}
  92. eval ${LOG_WRITE}
  93. fi
  94. fi
  95. fi
  96. # let i++
  97. # done
  98. }
  99. ############################
  100. checkWANIP