Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #############################################################################
- # Double checks stored IP is in a valid format and, if so, checks for any #
- # previous errors updating the domain. If errors are found, sends an email #
- # informing the owner that there are still unresolved errors from a #
- # previous attempt. If no errors are found, sends a POST request via curl #
- # to Google domains to update their dynamic DNS record with the system's #
- # new IP. Also emails owner the response from google for the latest POST #
- # request. #
- # #
- # #
- # Michael Rabinovsky 5-31-2017 #
- # Special thanks to Wedge Jarrad #
- #############################################################################
- ##File Settings##
- # Absolute path to the file which holds the current IP address.
- # Updated via the ipcheck.sh script with the current IP just before executing this script.
- ipfile="$HOME/.ipcheck.txt"
- # Absolute path to the file which holds the old IP address.
- # Used to check for errors from google.
- previousipfile="$HOME/.previousip.txt"
- # Absolute path to the file which should hold the responce from Google.
- # Updated via the the curl command when running this script.
- responsefile="$HOME/.ddnsresponse.txt"
- ##Email Settings##
- # The email address that will serve as both the source and destination for
- # emails sent by this script. It is also used as the username when
- # authenticating with the smtp server.
- email_address=""
- # Password used to authenticate with the smtp server.
- email_password=""
- # SMTP server to use.
- smtp_server=""
- # SMTP server port. We will be connecting via TLS.
- smtp_port=587
- # Will appear in the email message. Useful if you have this script running from more than one box.
- computer_name=""
- ##DNS Settings##
- # The domain we want to update.
- ddns_domain=""
- # The DDNS user given to us by Google.
- ddns_user=""
- # the DDNS password given to us by Google.
- ddns_password=""
- ##-----------------------##
- #The part that does work!##
- # Get the IP address, if any.
- touch "$ipfile"
- read -r ip < "$ipfile"
- #Get the old IP, if any.
- touch "$previousipfile"
- read -r previousip < "$previousipfile"
- # Stop now if we aren't able to retrieve valid IP addresses.
- # Not a very robust IP regex but should be good enough to filter out error messages and such (by Wedge Jarrad).
- [[ "$ip" =~ ^[0-9a-fA-F\.:]+$ ]] || exit 1
- [[ "$previousip" =~ ^[0-9a-fA-F\.:]+$ ]] || exit 1
- # Get the last response from google, if any.
- touch "$responsefile"
- read -r response < "$responsefile"
- # Send an email informing owner that the last response was an error, and stop the script.
- if [[ "$response" != "good $previousip" ]] && [[ "$response" != "nochg $previousip" ]]; then
- # RFC 2822 compliant SMTP email.
- email="ehlo localhost
- auth plain $(printf '\x00%s\x00%s' "$email_address" "$email_password" | base64)
- mail from: <$email_address>
- rcpt to: <$email_address>
- data
- from: $email_address
- subject: dynamic dns: $computer_name - pending error
- Google has responded with $response previously and the error was never cleared from the system.
- Nothing has been changed.
- The previous IP was $oldip. The new IP is $ip.
- .
- QUIT"
- slowly() {
- while read -r line; do
- sleep 1
- printf '%s\n' "$line"
- done <<< "$email"
- }
- slowly | openssl s_client -crlf -starttls smtp -connect "$smtp_server":"$smtp_port" > /dev/null 2>&1
- exit 1
- fi
- # Send the current IP address to Google.
- # Update the response file with the response from google.
- if [ command -v curl > /dev/null ]; then
- curl -L "https://$ddns_user:$ddns_password@domains.google.com/nic/update?hostname=$ddns_domain" > .ddnsresponse.txt
- else
- # don't have curl. Bail out.
- printf '%s' "This script requires curl to be installed."
- exit 1
- fi
- # Get the last response from google, if any.
- read -r response < "$responsefile"
- # Email the response to owner.
- # If response is anything other than a succesful IP change, send error email, else send success.
- if [[ "$response" != "good $ip" ]]; then
- # RFC 2822 compliant SMTP error email.
- email="ehlo localhost
- auth plain $(printf '\x00%s\x00%s' "$email_address" "$email_password" | base64)
- mail from: <$email_address>
- rcpt to: <$email_address>
- data
- from: $email_address
- subject: dynamic dns: $computer_name - update error
- $computer_name has tried to update the DNS record for $ddns_domain and got an error.
- Google has responded with $response.
- The previous IP was $previousip. The new IP is $ip.
- .
- QUIT"
- else
- # RFC 2822 compliant SMTP success email.
- email="ehlo localhost
- auth plain $(printf '\x00%s\x00%s' "$email_address" "$email_password" | base64)
- mail from: <$email_address>
- rcpt to: <$email_address>
- data
- from: $email_address
- subject: dynamic dns: $computer_name - update success!
- $computer_name has tried to update the DNS record for $ddns_domain and it was a total success.
- Google has responded with $response, which means that you are a baller, shot caller.
- The previous IP was $previousip. The new IP is $ip.
- .
- QUIT"
- fi
- slowly() {
- while read -r line; do
- sleep 1
- printf '%s\n' "$line"
- done <<< "$email"
- }
- 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