Last active
March 18, 2019 17:43
-
-
Save homebysix/ed1b3369bdb119e32f30 to your computer and use it in GitHub Desktop.
is_in_specified_subnet.sh
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 | |
### | |
# | |
# Name: is_in_specified_subnet.sh | |
# Description: A script that detects whether this Mac is on a particular | |
# subnet, as determined by whether the IP starts with a | |
# specific string. | |
# Author: Elliot Jordan <[email protected]> | |
# Created: 2015-09-21 | |
# Last Modified: 2015-09-21 | |
# Version: 1.0 | |
# | |
### | |
# The beginning segment of a matching IP address (e.g. "10.1.12.") | |
MATCH="10.1.12." | |
# Generate list of all active network device ports (e.g. en0, en1). | |
DEVICE_PORTS="$(ifconfig | awk -F: '/<UP,BROADCAST,SMART,RUNNING,/{print $1}' | sort 2> /dev/null) | |
$(ifconfig | awk -F: '/<UP,POINTOPOINT,RUNNING,/{print $1}' | sort 2> /dev/null)" | |
# Flag that switches to "true" if a match is found. | |
DOES_MATCH="false" | |
# Iterate through active ports. | |
for PORT in $DEVICE_PORTS; do | |
# Get IP address of this port. | |
THIS_IP=$(ifconfig "$PORT" | awk '/inet /{print $2}' 2> /dev/null) | |
if [[ "$THIS_IP" != "" ]]; then | |
if [[ "$THIS_IP" =~ ^$MATCH ]]; then | |
DOES_MATCH="true" | |
fi | |
fi | |
done | |
echo "$DOES_MATCH" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment