foxygit / sxbar Log in
commits tags

/scripts/network.sh · 1.66 KB

raw
#!/bin/sh
# network module -- everything the module needs, dispatched by subcommand:
#   network.sh           -> bar text, "Online"/"Offline" (default route exists?)
#   network.sh wifi      -> WiFi row: first wireless interface + its IPv4 (popup)
#   network.sh ethernet  -> Ethernet row: first physical wired interface + its IPv4 (popup)
#   network.sh menu      -> popup definition, read once at startup (see
#                           README, "Module-declared menus")
mode="${1:-status}"

case "$mode" in
menu)
	self="$0"
	echo 'popup : hover : buttons'
	echo "popup_info : \"$self wifi\""
	echo "popup_info : \"$self ethernet\""
	;;
status)
	ip route show default 2>/dev/null | grep -q . && echo Online || echo Offline
	;;
wifi)
	# first interface with a /sys/class/net/<if>/wireless directory
	i=$(for d in /sys/class/net/*/wireless; do
		[ -d "$d" ] && basename "$(dirname "$d")" && break
	done)

	if [ -z "$i" ]; then
		echo 'WiFi: none'
	else
		ip=$(ip -4 -o addr show "$i" 2>/dev/null | awk '{print $4}' | cut -d/ -f1)
		if [ -n "$ip" ]; then
			echo "WiFi ($i): $ip"
		else
			echo "WiFi ($i): disconnected"
		fi
	fi
	;;
ethernet)
	# first non-virtual, non-wireless interface of ARPHRD_ETHER type
	i=$(for d in /sys/class/net/*; do
		n=$(basename "$d")
		case "$n" in
			lo|docker*|veth*|br-*|virbr*|tun*|tap*) continue ;;
		esac
		[ -d "$d/wireless" ] && continue
		[ "$(cat "$d/type" 2>/dev/null)" = "1" ] && echo "$n" && break
	done)

	if [ -z "$i" ]; then
		echo 'Ethernet: none'
	else
		ip=$(ip -4 -o addr show "$i" 2>/dev/null | awk '{print $4}' | cut -d/ -f1)
		if [ -n "$ip" ]; then
			echo "Ethernet ($i): $ip"
		else
			echo "Ethernet ($i): disconnected"
		fi
	fi
	;;
esac