Use DNS to Find All IPs and Hostnames

My company includes the user’s name as one part of a workstation’s complete hostname. From time to time I need to lookup the IP or complete hostname for that user. There are several ways to do this, but I keep the below one-liners in ClipMenu. Having them within easy reach makes them a convenient method to get the required info, even if I’m SSHed into a remote network with otherwise limited access. As written, they work on a standard Class C network, scanning everything from .1 to .254. Change the net variable to match the network you’re scanning.

Resolve IPs

net=10.1.2;i=1; while [ $i -lt 255 ]; do line=$(nslookup $net.$i|grep name|awk -F" " '{ print $1 " " $4 }'); name=$(echo $line| awk '{ print $2 }');ip=$(echo $line| awk '{ print $1 }'|awk -F. '{ print $4 "." $3 "." $2 "." $1 }'); echo $name $ip; ((i++)); done | grep -v "\.\.\." | sort

Resolve Hostnames

net=10.1.2;i=1; while [ $i -lt 255 ]; do line=$(nslookup $net.$i|grep name|awk -F" " '{ print $1 " " $4 }'); name=$(echo $line| awk '{ print $2 }');ip=$(echo $line| awk '{ print $1 }'|awk -F. '{ print $4 "." $3 "." $2 "." $1 }'); echo $ip $name; ((i++)); done | grep -v "\.\.\."

This is a personal website. Unless otherwise stated, the content and opinions expressed here are my own and not those of my employer.