Files
omarchycn/bin/omarchy-network-status
T
9455496990 Fix style inconsistencies in bin/ (#7518)
* Use (( )) for the numeric argument test

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Drop the quotes on a variable inside [[ ]]

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Use omarchy-pkg-drop instead of raw pacman -Rns

omarchy-pkg-drop already filters to installed packages, so the
2>/dev/null || true suppression is no longer needed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Drop defensive checks around default-set commands

ttfx, imagemagick, and networkmanager are all in the default package
set, so their commands are runtime invariants and should be invoked
directly. Removing the nmcli guard also removes the degraded wifi
fallthrough that only ran when nmcli was missing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-19 20:16:47 +02:00

138 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# omarchy:summary=Print active network status for the shell
# omarchy:group=network
# omarchy:args=[--verbose]
verbose=false
internet_probe=1.1.1.1
case "${1:-}" in
"")
;;
--verbose)
verbose=true
;;
*)
echo "Usage: omarchy-network-status [--verbose]" >&2
exit 2
;;
esac
print_status() {
local device nm state ssid signal freq
device=$(ip route get "$internet_probe" 2>/dev/null | awk '{ for (i = 1; i <= NF; i++) if ($i == "dev") { print $(i + 1); exit } }')
if [[ -z $device ]]; then
printf 'disconnected\t\t\t\n'
return
fi
if [[ ! -d /sys/class/net/$device/wireless ]]; then
printf 'ethernet\t%s\t\t\n' "$device"
return
fi
nm=$(nmcli -t -f GENERAL.STATE,GENERAL.CONNECTION dev show "$device" 2>/dev/null)
state=$(awk -F: '$1 == "GENERAL.STATE" { print $2; exit }' <<<"$nm")
ssid=$(awk -F: '$1 == "GENERAL.CONNECTION" { print $2; exit }' <<<"$nm")
signal=$(nmcli -t -f IN-USE,SIGNAL dev wifi list ifname "$device" --rescan no 2>/dev/null | awk -F: '$1 == "*" { print $2; exit }')
freq=$(iw dev "$device" link 2>/dev/null | awk '/freq:/ { print $2; exit }')
if [[ $state != 100* ]]; then
printf 'disconnected\t\t\t\n'
return
fi
printf 'wifi\t%s\t%s\t%s\n' "${ssid:-$device}" "$signal" "$freq"
}
ping_latency_ms() {
local host=$1
LC_ALL=C ping -n -c 1 -W 1 "$host" 2>/dev/null | awk -F'time[=<]' '/time[=<]/ { split($2, parts, " "); print parts[1]; exit }'
}
print_ping_samples() {
local gateway=$1
local tmpdir router_file internet_file
local router_pid="" internet_pid=""
omarchy-cmd-present ping || return
tmpdir=$(mktemp -d) || return
router_file="$tmpdir/router"
internet_file="$tmpdir/internet"
if [[ -n $gateway ]]; then
ping_latency_ms "$gateway" >"$router_file" &
router_pid=$!
fi
ping_latency_ms "$internet_probe" >"$internet_file" &
internet_pid=$!
if [[ -n $router_pid ]]; then
wait "$router_pid"
printf 'router_ping_ms\t%s\n' "$(cat "$router_file")"
fi
wait "$internet_pid"
printf 'internet_ping_ms\t%s\n' "$(cat "$internet_file")"
rm -rf "$tmpdir"
}
print_verbose() {
local route_json iface gw src prefix link
route_json=$(ip -j route get "$internet_probe" 2>/dev/null)
[[ -z $route_json ]] && return
iface=$(jq -r '.[0].dev // ""' <<<"$route_json" 2>/dev/null)
gw=$(jq -r '.[0].gateway // ""' <<<"$route_json" 2>/dev/null)
src=$(jq -r '.[0].prefsrc // ""' <<<"$route_json" 2>/dev/null)
[[ -z $iface ]] && return
prefix=$(ip -j addr show "$iface" 2>/dev/null | jq -r '.[0].addr_info[]? | select(.family == "inet") | .prefixlen // ""' 2>/dev/null | head -n 1)
printf 'iface\t%s\n' "$iface"
printf 'ip\t%s\n' "$src"
printf 'prefix\t%s\n' "$prefix"
printf 'gateway\t%s\n' "$gw"
if [[ -r /sys/class/net/$iface/statistics/rx_bytes ]]; then
printf 'rx_bytes\t%s\n' "$(cat /sys/class/net/$iface/statistics/rx_bytes)"
fi
if [[ -r /sys/class/net/$iface/statistics/tx_bytes ]]; then
printf 'tx_bytes\t%s\n' "$(cat /sys/class/net/$iface/statistics/tx_bytes)"
fi
if [[ -d /sys/class/net/$iface/wireless ]]; then
printf 'type\twifi\n'
if omarchy-cmd-present iw; then
link=$(iw dev "$iface" link 2>/dev/null)
if [[ -n $link ]]; then
printf 'ssid\t%s\n' "$(awk '/SSID:/ { sub(/.*SSID: /, ""); print; exit }' <<<"$link")"
printf 'signal_dbm\t%s\n' "$(awk '/signal:/ { print $2; exit }' <<<"$link")"
printf 'freq\t%s\n' "$(awk '/freq:/ { print $2; exit }' <<<"$link")"
printf 'bitrate\t%s %s\n' "$(awk '/tx bitrate:/ { print $3; exit }' <<<"$link")" "$(awk '/tx bitrate:/ { print $4; exit }' <<<"$link")"
fi
fi
else
printf 'type\tethernet\n'
[[ -r /sys/class/net/$iface/speed ]] && printf 'speed\t%s\n' "$(cat /sys/class/net/$iface/speed)"
[[ -r /sys/class/net/$iface/duplex ]] && printf 'duplex\t%s\n' "$(cat /sys/class/net/$iface/duplex)"
fi
print_ping_samples "$gw"
}
if [[ $verbose == "true" ]]; then
print_verbose
else
print_status
fi