From d1f612bf791e1449287227fb8a355d151c62862f Mon Sep 17 00:00:00 2001 From: afranco Date: Sat, 30 Mar 2024 16:07:55 +0000 Subject: [PATCH] chore: EthernetStatus script and the LaunchCtl plist --- .local/bin/ethernetstatus | 58 +++++++++++++++++++++++ Library/LaunchAgents/ethernetstatus.plist | 20 ++++++++ 2 files changed, 78 insertions(+) create mode 100755 .local/bin/ethernetstatus create mode 100644 Library/LaunchAgents/ethernetstatus.plist diff --git a/.local/bin/ethernetstatus b/.local/bin/ethernetstatus new file mode 100755 index 0000000..84d9d23 --- /dev/null +++ b/.local/bin/ethernetstatus @@ -0,0 +1,58 @@ +#!/bin/bash + +get_adapter_name() { + adapter=$(/usr/sbin/networksetup -listnetworkserviceorder | /usr/bin/sed -En 's|^\(Hardware Port: AX88179A, Device: (en.)\)$|\1|p') + echo "$adapter" +} + +check_ethernet_status() { + adapter="$1" + status=$( /sbin/ifconfig "$adapter" | grep 'status: active' ) + if [ -n "$status" ]; then + echo "Ethernet on adapter $adapter is connected" + return 0 + else + echo "Ethernet on adapter $adapter is not connected" + return 1 + fi +} + +open_app() { + app="$1" + if ! pgrep -f "$app" >/dev/null; then + /usr/bin/open -a "$app" + fi +} + +close_app() { + app="$1" + if pgrep -f "$app" >/dev/null; then + pkill -f "$app" + fi +} + +main() { + if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + return 1 + fi + + adapter=$(get_adapter_name) + if [ -z "$adapter" ]; then + echo "Failed to get adapter name" + return 1 + fi + + app="$1" + + if check_ethernet_status "$adapter"; then + open_app "$app" + else + close_app "$app" + fi + + return 0 +} + +main "$@" + diff --git a/Library/LaunchAgents/ethernetstatus.plist b/Library/LaunchAgents/ethernetstatus.plist new file mode 100644 index 0000000..2fc1848 --- /dev/null +++ b/Library/LaunchAgents/ethernetstatus.plist @@ -0,0 +1,20 @@ + + + + + Label + net.francof.ethernetstatus + ProgramArguments + + /Users/afonso/.local/bin/ethernetstatus + Ethernet Status + + RunAtLoad + + WatchPaths + + /Library/Preferences/SystemConfiguration + + + +