Copybara bot | ca5ce64 | 2024-11-08 17:38:08 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # Copyright (C) 2014 Julien Bonjean <julien@bonjean.info> |
| 3 | # Copyright (C) 2014 Alexander Keller <github@nycroth.com> |
| 4 | |
| 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 | |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | #------------------------------------------------------------------------ |
| 19 | |
| 20 | # Use the provided interface, otherwise the device used for the default route. |
| 21 | IF="${IFACE:-$BLOCK_INSTANCE}" |
| 22 | IF="${IF:-$(ip route | awk '/^default/ { print $5 ; exit }')}" |
| 23 | |
| 24 | # Exit if there is no default route |
| 25 | [[ -z "$IF" ]] && exit |
| 26 | |
| 27 | #------------------------------------------------------------------------ |
| 28 | |
| 29 | # As per #36 -- It is transparent: e.g. if the machine has no battery or wireless |
| 30 | # connection (think desktop), the corresponding block should not be displayed. |
| 31 | [[ ! -d /sys/class/net/${IF} ]] && exit |
| 32 | |
| 33 | #------------------------------------------------------------------------ |
| 34 | |
| 35 | AF=${ADDRESS_FAMILY:-inet6?} |
| 36 | LABEL="${LABEL:-}" |
| 37 | |
| 38 | for flag in "$1" "$2"; do |
| 39 | case "$flag" in |
| 40 | -4) |
| 41 | AF=inet ;; |
| 42 | -6) |
| 43 | AF=inet6 ;; |
| 44 | -L) |
| 45 | if [[ "$IF" = "" ]]; then |
| 46 | LABEL="iface" |
| 47 | else |
| 48 | LABEL="$IF:" |
| 49 | fi ;; |
| 50 | esac |
| 51 | done |
| 52 | |
| 53 | if [[ "$IF" = "" ]] || [[ "$(cat /sys/class/net/$IF/operstate)" = 'down' ]]; then |
| 54 | echo "${LABEL} down" # full text |
| 55 | echo "${LABEL} down" # short text |
| 56 | echo \#FF0000 # color |
| 57 | exit |
| 58 | fi |
| 59 | |
| 60 | # if no interface is found, use the first device with a global scope |
| 61 | IPADDR=$(ip addr show $IF | perl -n -e "/$AF ([^ \/]+).* scope global/ && print \$1 and exit") |
| 62 | |
| 63 | case $BLOCK_BUTTON in |
| 64 | 3) echo -n "$IPADDR" | xclip -q -se c ;; |
| 65 | esac |
| 66 | |
| 67 | if [[ "${display_wifi_name}" == "1" ]]; |
| 68 | then |
| 69 | # try to guess the wifi name |
| 70 | if command -v iw > /dev/null && iw $IF info > /dev/null 2>&1; |
| 71 | then |
| 72 | WIFI_NAME=$(iw $IF info | grep -Po '(?<=ssid ).*' | tr -d " \t\n\r") |
| 73 | |
| 74 | if [[ $BLOCK_BUTTON -eq 1 ]]; |
| 75 | then |
| 76 | message="$LABEL $WIFI_NAME ($IPADDR)" |
| 77 | else |
| 78 | message="$LABEL $WIFI_NAME" |
| 79 | fi |
| 80 | else |
| 81 | message="$LABEL $IPADDR" |
| 82 | fi |
| 83 | else |
| 84 | message="$LABEL $IPADDR" |
| 85 | fi |
| 86 | |
| 87 | #------------------------------------------------------------------------ |
| 88 | |
| 89 | echo "$message" |