From Kromonos, 1 Year ago, written in Bash.
Embed
  1. #!/bin/bash
  2. # Redirect STDOUT/STDERR into syslog
  3. exec > >(logger -p user.info -t dyndns) 2> >(logger -p user.warn -t dyndns)
  4.  
  5. HOST="mail.error.cf"
  6. DNS="ns.bka.li"
  7. IPLOOKUP="http://ip.bka.li"
  8. MAILTO="hab@ich.neti"
  9. KEYFILE="/home/kromonos/.ssh/id_rsa"
  10. echo -n "IP lookup new: "
  11. IP=$(curl -s ${IPLOOKUP})
  12. echo "${IP}"
  13. echo -n "IP lookup old: "
  14. OLDIP=$(dig +short ${HOST} @${DNS})
  15. echo "${OLDIP}"
  16. SERVER=${DNS}
  17. PORT=22
  18.  
  19. if [ "${IP}" != "${OLDIP}" ]
  20. then
  21.     echo "Connecting to ${SERVER} using keyfile ${KEYFILE}"
  22.     ssh -p${PORT} -i ${KEYFILE} ${SERVER} "echo ${IP} > /home/kromonos/.home.ip";
  23.     DATE=$(date)
  24.     if [ "${?}" != "0" ]
  25.     then
  26.         echo "Error (${?}) while writing file onto server ${SERVER}"
  27.         echo "Connection error (${?}) to ${SERVER} at ${DATE}.\nIP (${$OLDIP}) not changed. New IP would be ${IP}!" | mail -s "[DynDNS] Connection error to ${SERVER}" ${MAILTO}
  28.         exit $?
  29.     fi
  30.     echo "New IP ${IP} written to file on ${SERVER}."
  31.     echo "Home IP change from ${OLDIP} to ${IP}. Message to ${SERVER} sent at ${DATE}" | mail -s "[DynDNS] IP change to ${IP}" ${MAILTO}
  32. else
  33.     echo "Nothing to do"
  34. fi
  35. exit 0
  36.