Last active
July 22, 2016 21:03
-
-
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
This file contains 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
#!/bin/sh | |
cat ips.txt | while read ip | |
do | |
echo $ip " " & host $ip | cut -f 5 -d " " | |
done |
This file contains 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
# 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 |
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:
- 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.
- the sort -u obviously sorts in order and only pulls out the unique IPs
- then you pipe it to "xargs", which makes it easier to pipe into a few system commands (maybe there's an easier way).
- 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
- 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)
- 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
- 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
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.
if it is always just the IPs only, this would work too:
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".