Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Base variables
- root="${HOME}/myip"
- file="${root}/$(hostname -f).txt"
- # Ensure root dir
- mkdir -p "${root}"
- # Ensure git repo
- cd "${root}"
- [[ -d "${root}/.git" ]] || {
- git init
- git remote add origin git@github.com:tzvetkoff/myip.git
- }
- # Fetch and reset to latest master
- git fetch origin master
- git reset --hard origin/master
- # Get date
- date=$(date '+%c')
- # Get public ip address
- new_ip=$(dig +short TXT o-o.myaddr.l.google.com @ns1.google.com | sed s/'"'//g)
- # Some error checking
- [[ -z "${new_ip}" ]] && exit 1
- # Parse old public ip address and check for changes
- if [[ -f "${file}" ]]; then
- old_ip=$(cat "${file}" | awk 'FNR == 2 { print }')
- if [[ "${new_ip}" == "${old_ip}" ]]; then
- exit 255
- fi
- fi
- # Create new file
- > "${file}"
- echo "${date}" >> "${file}"
- echo "${new_ip}" >> "${file}"
- # Add, commit, push
- git add .
- git commit -m 'Update'
- git push origin master
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement