SHARE
TWEET

Untitled

a guest Feb 14th, 2020 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # Base variables
  4. root="${HOME}/myip"
  5. file="${root}/$(hostname -f).txt"
  6.  
  7. # Ensure root dir
  8. mkdir -p "${root}"
  9.  
  10. # Ensure git repo
  11. cd "${root}"
  12. [[ -d "${root}/.git" ]] || {
  13.   git init
  14.   git remote add origin git@github.com:tzvetkoff/myip.git
  15. }
  16.  
  17. # Fetch and reset to latest master
  18. git fetch origin master
  19. git reset --hard origin/master
  20.  
  21. # Get date
  22. date=$(date '+%c')
  23. # Get public ip address
  24. new_ip=$(dig +short TXT o-o.myaddr.l.google.com @ns1.google.com | sed s/'"'//g)
  25.  
  26. # Some error checking
  27. [[ -z "${new_ip}" ]] && exit 1
  28.  
  29. # Parse old public ip address and check for changes
  30. if [[ -f "${file}" ]]; then
  31.   old_ip=$(cat "${file}" | awk 'FNR == 2 { print }')
  32.  
  33.   if [[ "${new_ip}" == "${old_ip}" ]]; then
  34.     exit 255
  35.   fi
  36. fi
  37.  
  38. # Create new file
  39. > "${file}"
  40. echo "${date}" >> "${file}"
  41. echo "${new_ip}" >> "${file}"
  42.  
  43. # Add, commit, push
  44. git add .
  45. git commit -m 'Update'
  46. git push origin master
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top