Skip to content

Instantly share code, notes, and snippets.

@stefanlasiewski
Created May 31, 2013 22:28
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 stefanlasiewski/5688376 to your computer and use it in GitHub Desktop.
Save stefanlasiewski/5688376 to your computer and use it in GitHub Desktop.
Shell script scripts to get hostnames from IPs and VIPs listed in ifconfig
#!/bin/sh
# Shell script scripts to get hostnames from IPs and VIPs listed in ifconfig
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Based off of http://bash.cyberciti.biz/misc-shell/read-local-ip-address/
PATH=$PATH:/sbin
# Get OS name
OS=`uname`
IPS="" # store IPS
case $OS in
Linux)
IPS=`ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
;;
FreeBSD|OpenBSD|Darwin)
IPS=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'`
;;
SunOS)
IPS=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '`
;;
*)
IPS="Unknown"
;;
esac
for IP in $IPS
do
# Need HNAME so that one entry prints per line, even for null records.
HNAME=`dig +short -x $IP`
echo "$IP = $HNAME"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment