Advertisement
Guest User

Untitled

a guest
May 31st, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.49 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # Double checks stored IP is in a valid format and, if so, checks for any   #
  4. # previous errors updating the domain. If errors are found, sends an email  #
  5. # informing the owner that there are still unresolved errors from a         #
  6. # previous attempt. If no errors are found, sends a POST request via curl   #
  7. # to Google domains to update their dynamic DNS record with the system's    #
  8. # new IP. Also emails owner the response from google for the latest POST    #
  9. # request.                                                                  #
  10. #                                                                           #
  11. #                                                                           #
  12. # Michael Rabinovsky        5-31-2017                                       #
  13. # Special thanks to Wedge Jarrad                                            #
  14. #############################################################################
  15.  
  16.  
  17.  
  18. ##File Settings##
  19.  
  20. # Absolute path to the file which holds the current IP address.
  21. # Updated via the ipcheck.sh script with the current IP just before executing this script.
  22. ipfile="$HOME/.ipcheck.txt"
  23.  
  24. # Absolute path to the file which holds the old IP address.
  25. # Used to check for errors from google.
  26. previousipfile="$HOME/.previousip.txt"
  27.  
  28. # Absolute path to the file which should hold the responce from Google.
  29. # Updated via the the curl command when running this script.
  30. responsefile="$HOME/.ddnsresponse.txt"
  31.  
  32.  
  33. ##Email Settings##
  34.  
  35. # The email address that will serve as both the source and destination for
  36. # emails sent by this script. It is also used as the username when
  37. # authenticating with the smtp server.
  38. email_address=""
  39.  
  40. # Password used to authenticate with the smtp server.
  41. email_password=""
  42.  
  43. # SMTP server to use.
  44. smtp_server=""
  45.  
  46. # SMTP server port. We will be connecting via TLS.
  47. smtp_port=587
  48.  
  49. # Will appear in the email message. Useful if you have this script running from more than one box.
  50. computer_name=""
  51.  
  52.  
  53. ##DNS Settings##
  54.  
  55. # The domain we want to update.
  56. ddns_domain=""
  57.  
  58. # The DDNS user given to us by Google.
  59. ddns_user=""
  60.  
  61. # the DDNS password given to us by Google.
  62. ddns_password=""
  63.  
  64.  
  65. ##-----------------------##
  66. #The part that does work!##
  67.  
  68. # Get the IP address, if any.
  69. touch "$ipfile"
  70. read -r ip < "$ipfile"
  71.  
  72. #Get the old IP, if any.
  73. touch "$previousipfile"
  74. read -r previousip < "$previousipfile"
  75.  
  76. # Stop now if we aren't able to retrieve valid IP addresses.
  77. # Not a very robust IP regex but should be good enough to filter out error messages and such (by Wedge Jarrad).
  78. [[ "$ip" =~ ^[0-9a-fA-F\.:]+$ ]] || exit 1
  79. [[ "$previousip" =~ ^[0-9a-fA-F\.:]+$ ]] || exit 1
  80.  
  81. # Get the last response from google, if any.
  82. touch "$responsefile"
  83. read -r response < "$responsefile"
  84.  
  85. # Send an email informing owner that the last response was an error, and stop the script.
  86. if [[ "$response" != "good $previousip" ]] && [[ "$response" != "nochg $previousip" ]]; then
  87.  
  88.         # RFC 2822 compliant SMTP email.
  89.         email="ehlo localhost
  90.         auth plain $(printf '\x00%s\x00%s' "$email_address" "$email_password" | base64)
  91.         mail from: <$email_address>
  92.         rcpt to: <$email_address>
  93.         data
  94.         from: $email_address
  95.         subject: dynamic dns: $computer_name - pending error
  96.         Google has responded with $response previously and the error was never cleared from the system.
  97.         Nothing has been changed.
  98.         The previous IP was $oldip. The new IP is $ip.
  99.         .
  100.         QUIT"
  101.        
  102.         slowly() {
  103.             while read -r line; do
  104.                 sleep 1
  105.                 printf '%s\n' "$line"
  106.             done <<< "$email"
  107.         }
  108.        
  109.         slowly | openssl s_client -crlf -starttls smtp -connect "$smtp_server":"$smtp_port" > /dev/null 2>&1
  110.        
  111.         exit 1
  112. fi
  113.  
  114. # Send the current IP address to Google.
  115. # Update the response file with the response from google.
  116. if [ command -v curl > /dev/null ]; then
  117.     curl -L "https://$ddns_user:$ddns_password@domains.google.com/nic/update?hostname=$ddns_domain" > .ddnsresponse.txt
  118. else
  119.     # don't have curl. Bail out.
  120.     printf '%s' "This script requires curl to be installed."
  121.     exit 1
  122.    
  123. fi
  124.  
  125. # Get the last response from google, if any.
  126. read -r response < "$responsefile"
  127.  
  128. # Email the response to owner.
  129. # If response is anything other than a succesful IP change, send error email, else send success.
  130. if [[ "$response" != "good $ip" ]]; then
  131.  
  132.         # RFC 2822 compliant SMTP error email.
  133.         email="ehlo localhost
  134.         auth plain $(printf '\x00%s\x00%s' "$email_address" "$email_password" | base64)
  135.         mail from: <$email_address>
  136.         rcpt to: <$email_address>
  137.         data
  138.         from: $email_address
  139.         subject: dynamic dns: $computer_name - update error
  140.         $computer_name has tried to update the DNS record for $ddns_domain and got an error.
  141.         Google has responded with $response.
  142.         The previous IP was $previousip. The new IP is $ip.
  143.         .
  144.         QUIT"
  145. else
  146.         # RFC 2822 compliant SMTP success email.
  147.         email="ehlo localhost
  148.         auth plain $(printf '\x00%s\x00%s' "$email_address" "$email_password" | base64)
  149.         mail from: <$email_address>
  150.         rcpt to: <$email_address>
  151.         data
  152.         from: $email_address
  153.         subject: dynamic dns: $computer_name - update success!
  154.         $computer_name has tried to update the DNS record for $ddns_domain and it was a total success.
  155.         Google has responded with $response, which means that you are a baller, shot caller.
  156.         The previous IP was $previousip. The new IP is $ip.
  157.         .
  158.         QUIT"      
  159.        
  160. fi
  161.  
  162. slowly() {
  163.             while read -r line; do
  164.                 sleep 1
  165.                 printf '%s\n' "$line"
  166.             done <<< "$email"
  167.         }
  168.        
  169.         slowly | openssl s_client -crlf -starttls smtp -connect "$smtp_server":"$smtp_port" > /dev/null 2>&1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement