Last active
January 26, 2016 21:46
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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