Skip to content

Instantly share code, notes, and snippets.

@dobbs
Last active December 4, 2016 22:40
Show Gist options
  • Save dobbs/6a24bb85b3c4b7d3a8d6c9aa1ba439ff to your computer and use it in GitHub Desktop.
Save dobbs/6a24bb85b3c4b7d3a8d6c9aa1ba439ff to your computer and use it in GitHub Desktop.
just gimme the IP address already
#!/bin/bash
#
# /usr/local/sbin/ipaddress
usage() {
cat <<EOF
Usage: $(basename $0) [interface]
Report the IP 4 address for the default interface (or the one given in the params)
(default interface is computed from the default route)
EOF
}
main() {
show_usage_and_exit_if_requested $@
local default=$(default_network_interface)
local interface=${1:-$default}
inet_addr_for_interface $interface
}
show_usage_and_exit_if_requested() {
if <<<"$@" egrep -q '(--help|-h)'; then
usage
exit 1
fi
}
default_network_interface() {
ip route | awk '/default/ {print $NF}'
}
inet_addr_for_interface() {
ifconfig $1 | awk -F'[ :]+' '/inet addr:/ {print $4}'
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment