Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.