Skip to content

Instantly share code, notes, and snippets.

@t-davies
Last active April 21, 2020 12:09
Show Gist options
  • Save t-davies/5d69c15c3d53a2bb05e9ab892d3c55e7 to your computer and use it in GitHub Desktop.
Save t-davies/5d69c15c3d53a2bb05e9ab892d3c55e7 to your computer and use it in GitHub Desktop.
Watches a *nix network interface for status changes
#! /bin/bash
watch_for_interface()
{
if (ifconfig "$1" | grep -q "status: active")
then
echo "[$1] state changed: active"
echo "...do stuff here..."
else
echo "[$1] state changed: inactive"
fi
}
INTERFACE=en0
export -f watch_for_interface
export INTERFACE
(echo -e "n.add State:/Network/Interface/${INTERFACE}/IPv4\nn.watch" & cat) | \
scutil | \
awk '/notification/ {system("watch_for_interface $INTERFACE")}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment