local_ip_check.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. ## This is my (demuredemeanor) polybar script for checking Local IP
  3. ##
  4. ## This script checks my computer's local ip, handles both eth and wlan, and handles hiding the ip.
  5. ## In addition it will indicate errors, lack of connection, and wifi signal strength.
  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. TOGGLE_SHOW_FILE="/tmp/i3_polybar_ip_toggle_${USER}"
  11. TOGGLE_IPV6_FILE="/tmp/i3_polybar_ipv6_toggle_${USER}"
  12. ## Get Data
  13. ## Contains docker, qemu, tun/tap interface mitigation
  14. LOCAL_IP="$(ip address show up scope global | awk '/inet[^6]/&&!/(docker|virbr|tun|tap|wg)[0-9]+$/ {match($2, /(.*)\/.*/, m); print m[1]}')"
  15. LOCAL_IPV6="$(ip address show up scope global | awk '/inet6/&&!/(docker|virbr|tun|tap|wg)[0-9]+$/ {match($2, /(.*)\/.*/, m); print m[1]}')"
  16. WIFI_SIG="$(iwconfig 2>/dev/null | awk '/Link/ {match($2, /\w+=([0-9]+)\/([0-9]+)/, m)} END {if(m[1]!=""&&m[2]!=""){print int((m[1] / m[2]) * 100)}}')"
  17. ## Set connection icon
  18. if [ "${LOCAL_IP}" != "" ]; then
  19. if [ "${WIFI_SIG}" = "" ]; then
  20. ## Set eth icon
  21. ICON=""
  22. else
  23. ## Set wlan icon
  24. ICON=""
  25. fi
  26. else
  27. ## Set no ip icon
  28. ICON=""
  29. fi
  30. ## Set no-connection output
  31. if [ "${LOCAL_IP}" = "" ]; then
  32. LOCAL_IP="×"
  33. fi
  34. ## Prep wifi signal display, deals with icon color
  35. if [ "${WIFI_SIG}" != "" ]; then
  36. WIFI_SIG="%{F#979997}%{O2}%{O2}%{F#C5C8C6}${WIFI_SIG}%%{O2}"
  37. fi
  38. ## Set no-ipv6 message. Thought process is that ipv4 is default, and it is possible to have ipv4 sans ipv6
  39. if [ "${LOCAL_IPV6}" = "" ]; then
  40. LOCAL_IPV6="No IPv6"
  41. fi
  42. ## Check Toggle IPv6 State
  43. if [ -s "${TOGGLE_IPV6_FILE}" ]; then
  44. TOGGLE_IPV6="$(cat "${TOGGLE_IPV6_FILE}")"
  45. if [ "${TOGGLE_IPV6}" -eq 1 ]; then
  46. LOCAL_IP="${LOCAL_IPV6}"
  47. fi
  48. fi
  49. ## Check Toggle Show State
  50. if [ -s "${TOGGLE_SHOW_FILE}" ]; then
  51. TOGGLE_SHOW="$(cat "${TOGGLE_SHOW_FILE}")"
  52. if [ "${TOGGLE_SHOW}" -eq 1 ]; then
  53. LOCAL_IP=""
  54. fi
  55. fi
  56. ## Return results
  57. ## Deals with icon color.
  58. echo "%{F#979997}%{O2}${ICON}%{O2}%{F#C5C8C6}${LOCAL_IP}%{O2}${WIFI_SIG}"