Guest User

Untitled

a guest
May 24th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/bin/sh
  2. # Check external IP for change
  3. # Ideal for use in a cron job
  4. #
  5. # Usage: sh check-ext-ip.sh
  6. #
  7. # Returns: Nothing if the IP is same, or the new IP address
  8. # First run always returns current address
  9. #
  10. # Requires dig:
  11. # Debian/Ubuntu: apt install dnsutils
  12. # Solus: eopkg install bind-utils
  13. # CentOS/Fedora: yum install bind-utils
  14. #
  15. # by Sina Mashek <sina@sinacutie.stream>
  16. # Released under CC0 or Public Domain, whichever is supported
  17.  
  18. # Where we will store the external IP
  19. EXT_IP="$HOME/.external-ip"
  20.  
  21. # Check if dig is installed
  22. if [ "$(command -v dig)" = "" ]; then
  23. echo "This script requires 'dig' to run"
  24.  
  25. # Load distribution release information
  26. . /etc/os-release
  27.  
  28. # Check for supported release; set proper package manager and package name
  29. if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then
  30. MGR="apt"
  31. PKG="dnsutils"
  32. elif [ "$ID" = "fedora" ] || [ "$ID" = "centos" ]; then
  33. MGR="yum"
  34. PKG="bind-utils"
  35. elif [ "$ID" = "solus" ]; then
  36. MGR="eopkg"
  37. PKG="bind-utils"
  38. else
  39. echo "Please consult your package manager for the correct package"
  40. exit 1
  41. fi
  42.  
  43. # Will run if one of the above supported distributions was found
  44. echo "Installing $PKG ..."
  45. sudo "$MGR" install "$PKG"
  46. fi
  47.  
  48. # We check our external IP directly from a DNS request
  49. GET_IP="$(dig +short myip.opendns.com @resolver1.opendns.com)"
  50.  
  51. # Check if ~/.external-ip exists
  52. if [ -f "$EXT_IP" ]; then
  53. # If the external ip is the same as the current ip
  54. if [ "$(cat "$EXT_IP")" = "$GET_IP" ]; then
  55. exit 0
  56. fi
  57. # If it doesn't exist or is not the same, grab and save the current IP
  58. else
  59. echo "$GET_IP" > "$EXT_IP"
  60. fi
Add Comment
Please, Sign In to add comment