Skip to content

Instantly share code, notes, and snippets.

@homebysix
Last active March 18, 2019 17:43
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 homebysix/ed1b3369bdb119e32f30 to your computer and use it in GitHub Desktop.
Save homebysix/ed1b3369bdb119e32f30 to your computer and use it in GitHub Desktop.
is_in_specified_subnet.sh
#!/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 <elliot@elliotjordan.com>
# 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