Skip to content

Instantly share code, notes, and snippets.

@StefanHamminga
Last active January 26, 2016 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanHamminga/0bba44b8bbd1bf37e781 to your computer and use it in GitHub Desktop.
Save StefanHamminga/0bba44b8bbd1bf37e781 to your computer and use it in GitHub Desktop.
Bash script to test if connected to a specific network and (optionally) if a given host is available on said network. Useful for cron / automated scripts.
#!/bin/bash -e
if [ $# -lt 1 ]; then
echo -e "
Connection and host availability test
Usage:\n$0 'My network name' [ip_or_hostname] && command_to_run_when_available
The network name needs to be quoted and as Network Manager reports it. Try
'nmcli c show'.
Optionally an IP or hostname can be checked. Three successive succesful pings
are considered a reliable link.
Your current networks:
"
nmcli c show
exit 1
fi
if [ $# -gt 1 ]; then
nmcli -t -f NAME c show --active | grep -qse "^${1}$" &&
ping -c 3 -i 0.333 -w 2 "$2" > /dev/null
exit $?
else
nmcli -t -f NAME c show --active | grep -qse "^${1}$"
exit $?
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment