Skip to content

Instantly share code, notes, and snippets.

@ryanb8
Last active December 28, 2023 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanb8/6454a5c42af92f22e182537fde30682f to your computer and use it in GitHub Desktop.
Save ryanb8/6454a5c42af92f22e182537fde30682f to your computer and use it in GitHub Desktop.
xbar plugin - Print active internet interface
#!/usr/bin/env bash
# A simple plugin for xbar to print the interface you are conntected to the internet with.
# See: https://github.com/matryer/xbar and https://xbarapp.com/
# Probably could be a plugin in xbar repo, TBD.
# Use case - sometimes my mac uses wifi when it shoudl use ethernet.
# Instructions:
# Name `ethernet_or_wifi.5m.sh`
# chmod +x it
# Thanks to: https://apple.stackexchange.com/questions/191879/how-to-find-the-currently-connected-network-service-from-the-command-line#comment635764_433612
default_route=$(route get default)
if [[ "$default_route" == '' ]]; then
source="Unconnected"
else
networks=$(networksetup -listnetworkserviceorder)
interface=$(echo "$default_route" | awk '/interface/ {print $2}' )
source=$(echo "$networks" | awk -v interface="$interface" "/$interface/{sub(/\([0-9]+\)\ /,\"\",a); print a} {a=\$0}")
fi
echo "$source"
@ryanb8
Copy link
Author

ryanb8 commented Dec 28, 2023

In my menu bar:

image

or

image

or

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment