Advertisement
jessicakennedy1028

IPAddress

Nov 26th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function valid_ip()
  4. {
  5.     local  ip=$1
  6.     local  stat=1
  7.  
  8.     if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  9.         OIFS=$IFS
  10.         IFS='.'
  11.         ip=($ip)
  12.         IFS=$OIFS
  13.         [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
  14.             && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
  15.         stat=$?
  16.     fi
  17.     return $stat
  18. }
  19.  
  20. IP=`dig +short myip.opendns.com @resolver1.opendns.com`
  21. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  22. LOGFILE="$DIR/update-ip.log"
  23. IPFILE="$DIR/update-ip.ip"
  24.  
  25. if ! valid_ip $IP; then
  26.    echo "Invalid IP address: $IP" >> "$LOGFILE"
  27.    exit 1
  28. fi
  29.  
  30. if [ ! -f "$IPFILE" ]
  31.    then
  32.    touch "$IPFILE"
  33. fi
  34.  
  35. if grep -Fxq "$IP" "$IPFILE"; then
  36.    # code if found
  37.    echo "IP is still $IP. Exiting" >> "$LOGFILE"
  38.    exit 0
  39. else
  40.    echo "IP has changed to $IP" >> "$LOGFILE"
  41.    TMPFILE=$(mktemp /tmp/temporary-file.XXXXXXXX)
  42.    cat > ${TMPFILE} << EOF
  43.    {
  44.      "Comment":"$COMMENT",
  45.      "Changes":[
  46.        {
  47.          "Action":"UPSERT",
  48.          "ResourceRecordSet":{
  49.            "ResourceRecords":[
  50.              {
  51.                "Value":"$IP"
  52.              }
  53.            ],
  54.            "Name":"$RECORDSET",
  55.            "Type":"$TYPE",
  56.            "TTL":$TTL
  57.          }
  58.        }
  59.      ]
  60.    }
  61. EOF
  62.  
  63.    # Do the actual send to google script and tell them the new IP address
  64.    echo file://"$TMPFILE" >> "$LOGFILE"
  65.    echo "" >> "$LOGFILE"
  66.    rm $TMPFILE
  67. fi
  68.  
  69. # All Done - cache the IP address for next time
  70. echo "$IP" > "$IPFILE"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement