ext_ip_check.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. ## This is my (demuredemeanor) polybar script for checking External IP
  3. ##
  4. ## This script checks my computer's external ip, limits the request rate, and handles hiding the ip.
  5. ## In addition it will indicate errors, lack of connection, and if the local and external ip match.
  6. ## Hiding is convenient when taking screen shots.
  7. ## Toggle with:
  8. ## awk -v TEMP=/tmp/i3_polybar_ip_toggle_${USER} 'BEGIN {{FILE=getline < TEMP < 0 ? "0" : "1"} {if($0==1){STATE=1} else {STATE=0}} {if(STATE==0){system("echo 1 > "TEMP)} else {system("echo 0 > "TEMP)}} }'
  9. ## Variables
  10. TEMP_FILE="/tmp/i3_polybar_ext_ip_${USER}"
  11. TOGGLE_SHOW_FILE="/tmp/i3_polybar_ip_toggle_${USER}"
  12. WAIT="660" ## In seconds. dyndns requires a minimum of 10 min between requests.
  13. CUR_TIME="$(date +%s)"
  14. UPDATE=0 ## Default to no update
  15. ## Test if an update is needed, or uses the stored external ip
  16. if [ -s "${TEMP_FILE}" ]; then
  17. FILE_TIME="$(stat -c %Y "${TEMP_FILE}")"
  18. TIME_DIFF="$(expr ${CUR_TIME} - ${FILE_TIME})"
  19. if [ "${TIME_DIFF}" -lt "${WAIT}" ]; then
  20. EXT_IP="$(cat "${TEMP_FILE}")"
  21. ## Catch bad state
  22. if [ "${EXT_IP}" = "" ]; then
  23. UPDATE=1
  24. fi
  25. else
  26. UPDATE=1 ## Run Update
  27. fi
  28. else
  29. UPDATE=1 ## Run Update
  30. fi
  31. ## Refreshes external ip
  32. if [ ${UPDATE} -eq 1 ]; then
  33. ## get ext ip
  34. EXT_IP="$(wget --no-proxy http://checkip.dyndns.org/ -q -O - | awk '{match($0, /Address: ([0-9.]+)/,m); if(m[1]!=""){print m[1]} else {print "err"}}')"
  35. ## Write to temp file
  36. echo "${EXT_IP}" > "${TEMP_FILE}"
  37. fi
  38. ## Set error icon
  39. if [ "${EXT_IP}" = "err" ]; then
  40. EXT_IP=""
  41. fi
  42. ## Set no-connection output, and save space if local and ext match.
  43. ## Contains docker, qemu, tun/tap interface mitigation
  44. LOCAL_IP="$(ip address show up scope global | awk '/inet[^6]/&&!/(docker|virbr|tun|tap)[0-9]+$/ {match($2, /(.*)\/.*/, m); print m[1]}')"
  45. if [ "${LOCAL_IP}" = "" ]; then
  46. EXT_IP="×"
  47. elif [ "${LOCAL_IP}" = "${EXT_IP}" ]; then
  48. EXT_IP="same"
  49. fi
  50. ## Check Toggle Show State
  51. if [ -s "${TOGGLE_SHOW_FILE}" ]; then
  52. TOGGLE_SHOW="$(cat "${TOGGLE_SHOW_FILE}")"
  53. if [ "${TOGGLE_SHOW}" -eq 1 ]; then
  54. ## Hide External IP
  55. EXT_IP=""
  56. fi
  57. fi
  58. ## Return results
  59. echo "${EXT_IP}"