aboutsummaryrefslogtreecommitdiff
path: root/config/i3blocks/scripts/iface
diff options
context:
space:
mode:
Diffstat (limited to 'config/i3blocks/scripts/iface')
-rwxr-xr-xconfig/i3blocks/scripts/iface61
1 files changed, 42 insertions, 19 deletions
diff --git a/config/i3blocks/scripts/iface b/config/i3blocks/scripts/iface
index abac9c0..ffa576e 100755
--- a/config/i3blocks/scripts/iface
+++ b/config/i3blocks/scripts/iface
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash
# Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>
# Copyright (C) 2014 Alexander Keller <github@nycroth.com>
@@ -17,38 +17,61 @@
#------------------------------------------------------------------------
-# Locate current default interface, but use only wlp (wifi) or enp (ethernet)
-# TODO probably also add connection over phone
-IF=$(ip route | awk '/^default .* (wlp|wlo|enp)/ { print $5 ; exit }')
+# Use the provided interface, otherwise the device used for the default route.
+IF="${IFACE:-$BLOCK_INSTANCE}"
+for version in 4 6; do
+ IF="${IF:-$(ip -$version route | sed -n 's/^default.*dev \([^ ]*\).*/\1/p')}"
+done
+
+# Exit if there is no default route
+[[ -z "$IF" ]] && exit
#------------------------------------------------------------------------
# As per #36 -- It is transparent: e.g. if the machine has no battery or wireless
# connection (think desktop), the corresponding block should not be displayed.
[[ ! -d /sys/class/net/${IF} ]] && exit
-# Note this passes if IF is empty!
#------------------------------------------------------------------------
-# If no IF is selected then we want see down, not anything else.
-if [ -z "$IF" ] || [[ "$(cat /sys/class/net/$IF/operstate)" = 'down' ]]; then
+AF=${ADDRESS_FAMILY:-"inet6\?"}
+LABEL="${LABEL:-}"
+
+for flag in "$1" "$2"; do
+ case "$flag" in
+ -4)
+ AF=inet ;;
+ -6)
+ AF=inet6 ;;
+ -L)
+ if [[ "$IF" = "" ]]; then
+ LABEL="iface"
+ else
+ LABEL="$IF:"
+ fi ;;
+ esac
+done
+
+if [[ "$IF" = "" ]] || [[ "$(cat /sys/class/net/$IF/operstate)" = 'down' ]]; then
+ echo "${LABEL} down" # full text
+ echo "${LABEL} down" # short text
+ echo \#FF0000 # color
exit
fi
-case $1 in
- -4)
- AF=inet ;;
- -6)
- AF=inet6 ;;
- *)
- AF=inet6? ;;
-esac
-
-# Use the first global scope address
-IPADDR=$(ip addr show $IF | perl -n -e "/$AF ([^\/]+).* scope global/ && print \$1 and exit")
+# Use the first global scope address that matches limitation and preffer not local one)
+while read -r ipaddr; do
+ case "$ipaddr" in
+ ""|169\.*|f*)
+ IPADDR="${IPADDR:-$ipaddr}"
+ continue # try another one
+ esac
+ IPADDR="$ipaddr"
+ break
+done <<<"$(ip addr show "$IF" | sed -n 's/.*inet6\? \([^/]\+\).* scope global.*/\1/p')"
case $BLOCK_BUTTON in
- 3) echo -n "$IPADDR" | xclip -q -se c ;;
+ 3) echo -n "$IPADDR" | xclip -q -se c ;;
esac
#------------------------------------------------------------------------