Advertisement
eibgrad

ddwrt-wanip-notify-331321.sh

Jan 21st, 2022 (edited)
700
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. # version: 1.0.0, 21-jan-2022, by eibgrad
  3. # href: https://tinyurl.com/3w4mfv8b
  4.  
  5. SCRIPTS_DIR='/jffs/etc/config'
  6. SCRIPT="$SCRIPTS_DIR/ddwrt-wanip-notify.startup"
  7.  
  8. mkdir -p $SCRIPTS_DIR
  9.  
  10. cat << 'EOF' > $SCRIPT
  11. #!/bin/sh
  12. DEBUG=; set -x # comment/uncomment to disable/enable debug mode
  13. (
  14. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  15.  
  16. # how often to check (in secs) for a change in wan ip
  17. INTERVAL=300
  18.  
  19. # smtp options
  20. USER='sender@gmail.com'
  21. PASS='XXXXXXXX'
  22. FROM_NAME='sender@gmail.com'
  23. TO='recipient@gmail.com'
  24. TO_NAME='recipient@gmail.com'
  25. SERVER='smtps://smtp.gmail.com:465'
  26. SUBJECT='WAN IP has changed!'
  27.  
  28. # ------------------------------- END OPTIONS -------------------------------- #
  29.  
  30. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  31.  
  32. MESSAGE="/tmp/tmp.$$.message"
  33.  
  34. format_message() {
  35. cat << EOM > $MESSAGE
  36. From: "$FROM_NAME" <$USER>
  37. To: "$TO_NAME" <$TO>
  38. Subject: $SUBJECT
  39.  
  40. Current  WAN IP: $curr_wan_ip
  41. Previous WAN IP: $prev_wan_ip
  42. EOM
  43. }
  44.  
  45. send_message() {
  46.     format_message
  47.  
  48.     curl --ssl-reqd \
  49.          --url "$SERVER" \
  50.          --user "$USER:$PASS" \
  51.          --mail-from "$USER" \
  52.          --mail-rcpt "$TO" \
  53.          --upload-file "$MESSAGE" \
  54.          $([ ${DEBUG+x} ] || echo '--silent')
  55. }
  56.  
  57. prev_wan_ip='N/A'
  58.  
  59. # wait for *reliable* internet connection
  60. until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
  61.  
  62. while :; do
  63.     curr_wan_ip="$(nvram get wan_ipaddr)"
  64.  
  65.     # send notification if wan ip changes
  66.     if [ "$curr_wan_ip" != "$prev_wan_ip" ]; then
  67.         echo "info: wan ip changed to $curr_wan_ip"
  68.  
  69.         if send_message; then
  70.             echo "info: notification sent to $TO"
  71.         else
  72.             echo "error: failed to send notification ($?)"
  73.         fi
  74.  
  75.         prev_wan_ip="$curr_wan_ip"
  76.     fi
  77.  
  78.     sleep $INTERVAL
  79. done
  80.  
  81. ) 2>&1 | logger $([ ${DEBUG+x} ] && echo '-p user.debug') \
  82.     -t $(echo $(basename $0) | grep -Eo '^.{0,23}')[$$] &
  83. EOF
  84. chmod +x $SCRIPT
  85. echo "installed: $SCRIPT"
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement