EthernetStatus script and the LaunchCtl plist

This commit is contained in:
Afonso Franco 2024-03-30 16:07:55 +00:00
parent 39dbb51857
commit f9b79f5974
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
2 changed files with 78 additions and 0 deletions

58
.local/bin/ethernetstatus Executable file
View file

@ -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 <program_to_run>"
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 "$@"

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>net.francof.ethernetstatus</string>
<key>ProgramArguments</key>
<array>
<string>/Users/afonso/.local/bin/ethernetstatus</string>
<string>Ethernet Status</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
</dict>
</plist>