Last active
May 5, 2018 21:09
-
-
Save darrenpmeyer/746f1f7626302de5eb0924794538dab3 to your computer and use it in GitHub Desktop.
Get IPv4/netmask network spec (e.g. 192.168.0.0/24) for current network, useful for nmap etc.
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
#!/usr/bin/env bash | |
############ | |
# run $ ./getnet.sh [iface] | |
# | |
# defaults to first non-local interface, but you can specify an interface. | |
# prints IP/Network in form 0.0.0.0/0 to STDOUT, so $(./getnet.sh en0) works | |
# | |
# e.g. run $ nmap -sP $(./getnet.sh en0) | |
# ^ will pingscan your current wifi network on macOS | |
# | |
# Works with macOS and most Linux distros including Ubuntu | |
# REQUIRES `ipcalc` be installed! `brew install ipcalc` or `sudo apt install ipcalc` | |
iface=$(ifconfig | egrep "^\S+:" | grep -v lo | grep UP | head -n 1 | cut -d ':' -f 1) | |
if [ -n "$1" ]; then | |
iface="$1" | |
shift | |
fi | |
>&2 echo "Using interface '$iface'" | |
if ! (which ipcalc > /dev/null); then | |
>&2 echo "ipcalc not installed" | |
exit 1 | |
fi | |
addrstr=$(ifconfig ${iface} | egrep "inet\s+[0-9]+\.") | |
# >&2 echo "INET: $addrstr" | |
ip=$(echo "$addrstr" | awk '{print $2}') | |
nm=$(echo "$addrstr" | awk '{print $4}') | |
# >&2 echo "$ip/$nm" | |
ipnet=$(ipcalc "$ip/$nm" | egrep "Network:\s" | awk '{print $2}') | |
echo $ipnet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment