iface 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>
  3. # Copyright (C) 2014 Alexander Keller <github@nycroth.com>
  4. # Copyright (C) 2020 Jesús E. <heckyel@hyperbola.info>
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #------------------------------------------------------------------------
  16. # Use the provided interface, otherwise the device used for the default route.
  17. IF="${IFACE:-$BLOCK_INSTANCE}"
  18. IF="${IF:-$(ip route | awk '/^default/ { print $5 ; exit }')}"
  19. # Exit if there is no default route
  20. [[ -z "$IF" ]] && exit
  21. #------------------------------------------------------------------------
  22. # As per #36 -- It is transparent: e.g. if the machine has no battery or wireless
  23. # connection (think desktop), the corresponding block should not be displayed.
  24. [[ ! -d /sys/class/net/${IF} ]] && exit
  25. #------------------------------------------------------------------------
  26. LABEL="${LABEL:-}"
  27. for flag in "$1" "$2"; do
  28. case "$flag" in
  29. -L)
  30. if [[ "$IF" = "" ]]; then
  31. LABEL="iface "
  32. else
  33. LABEL="$IF: "
  34. fi ;;
  35. esac
  36. done
  37. if [[ "$IF" = "" ]] || [[ "$(cat "/sys/class/net/$IF/operstate")" = 'down' ]];
  38. then
  39. echo "${LABEL}down" # text
  40. echo \#FF0000 # color
  41. exit
  42. fi
  43. # Check local ip address
  44. IPADDR=$(ip route get 4.2.2.1 | grep -oE "src [.0-9]+" | awk '{print $NF;exit}')
  45. case $BLOCK_BUTTON in
  46. 3) echo -n "$IPADDR" | xclip -q -se c ;;
  47. esac
  48. #------------------------------------------------------------------------
  49. echo "$LABEL$IPADDR"