Skip to content

Instantly share code, notes, and snippets.

@peasead
Last active July 22, 2016 21:03
Show Gist options
  • Save peasead/dbff8df19b4dfed6b813 to your computer and use it in GitHub Desktop.
Save peasead/dbff8df19b4dfed6b813 to your computer and use it in GitHub Desktop.
Simple script to loop through a list of IP addresses and output their resolutions
#!/bin/sh
cat ips.txt | while read ip
do
echo $ip " " & host $ip | cut -f 5 -d " "
done
# just add 1 IP per line
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
@csawall
Copy link

csawall commented Jun 28, 2016

maybe you can do what you doing in a while loop in one line? if the IP is first (or you could move the var), you could use this; even if it only has IPs in a return delimited file.

awk -F " " '{print $1}' file.txt |sort -u | xargs -I % sh -c 'echo %; host %'

if it is always just the IPs only, this would work too:

awk '{print $0}' file.txt |sort -u | xargs -I % sh -c 'echo %; host %'

not sure using "cut" will always work. the response is not always in the same spot and i have had various results where it'll just respond with "for" or "record".

@csawall
Copy link

csawall commented Jul 22, 2016

ok, i was playing around with this a little more. and it really depends on the output of the commands on how easy this can be. i think this new version may work best.

awk -F " " '{print $1}' file.txt |sort -u | xargs -I % sh -c 'printf "% "; dig +short -x % |xargs |grep . && continue || echo "NXDOMAIN"'

Breakdown:

  1. for the first awk, same info as i had above. if the IP is in a known location, this should be easy to set. if the file is only IP addresses, then you wouldn't need the -F and you can print $0.
  2. the sort -u obviously sorts in order and only pulls out the unique IPs
  3. then you pipe it to "xargs", which makes it easier to pipe into a few system commands (maybe there's an easier way).
  4. i used printf so that it would just print the IP address and not a newline character (for some reason, i couldn't get "echo -n" to work
  5. i then settled on "dig +short" so that the response was consistent, and easier to read. the downside is that if there is no result, it doesn't return anything (hence the upcoming grep statement)
  6. the 2nd xargs is really there for when dig responds with multiple domains tied to the IP in question - it will keep all results on the same line, space delimited, vs. a newline
  7. finally, i wanted to easily know when dig did not find a result. so i piped the output to grep and just searched for a dot. if there was a response from dig, it'll be found and the script will continue. if it is not found, it'll print out NXDOMAIN, and then continue on

all in all, it's still a nice one-liner so you don't have to have a script file to run and it's pretty fast. output looks roughly like this:

10.11.12.13 NXDOMAIN
10.22.33.44 10-22-33-44.somedomain.net
10.44.22.55 10-44-22-55.someotherdomain.net specialsystem.domain.net

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment