Created
November 24, 2016 02:17
-
-
Save mscalora/cabeb04659957c40a4451d84e3ac6df2 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "" ]] ; then | |
printf "Usage:\n\tonsubnet [ --not ] partial-ip-address\n\n" | |
printf "Example:\n\tonsubnet 10.10.\n\tonsubnet --not 192.168.0.\n\n" | |
printf "Note:\n\tThe partial-ip-address must match starting at the first\n" | |
printf "\tcharacter of the ip-address, therefore the first example\n" | |
printf "\tabove will match 10.10.10.1 but not 110.10.10.1\n" | |
printf "\tSee: http://stackoverflow.com/questions/40746463/how-to-automatically-switch-ssh-config-based-on-local-subnet\n" | |
exit 0 | |
fi | |
on=0 | |
off=1 | |
if [[ "$1" == "--not" ]] ; then | |
shift | |
on=1 | |
off=0 | |
fi | |
regexp="^$(sed 's/\./\\./g' <<<"$1")" | |
if [[ "$(uname)" == "Darwin" ]] ; then | |
ifconfig | fgrep 'inet ' | fgrep -v 127.0.0. | cut -d ' ' -f 2 | egrep "$regexp" >/dev/null | |
else | |
hostname -I | tr -s " " "\012" | fgrep -v 127.0.0. | egrep "$regexp" >/dev/null | |
fi | |
if [[ $? == 0 ]]; then | |
exit $on | |
else | |
exit $off | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment