Last active
April 2, 2024 11:06
-
-
Save gfoss/4453950 to your computer and use it in GitHub Desktop.
Basic nslookup loops for Windows and Linux
This file contains hidden or 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
*****WINDOWS***** | |
//nslookup - subnet range | |
c:\>for /L %i in (1,1,255) do @nslookup 10.10.10.%i [server to resolve from] 2>nul | find "Name" && echo 10.10.10.%i && @echo [ctrl+g] | |
//nslookup - file of ip's | |
NAME c:\>for /F %i in ([file.txt]) do @nslookup %i [server to resolve from] 2>nul | find "Name" && echo %i | |
ADDRESS c:\>for /F %i in ([file.txt]) do @nslookup %i [server to resolve from] 2>nul | find "Address" && echo %i | |
Or just run c:\>nslookup and paste in the list | |
This does not have as clean of output as the for loop though | |
******************** | |
*****LINUX***** | |
#script | |
#!/bin/sh | |
for IP in `cat ./ips.txt` | |
do | |
printf "$IP\t" | |
LOOKUP_RES=`nslookup $IP` | |
FAIL_COUNT=`echo $LOOKUP_RES | grep "** server can't find " | wc -l`; | |
if [ $FAIL_COUNT -eq 1 ] | |
then | |
NAME='Bad FQDNS\n'; | |
else | |
NAME=`echo $LOOKUP_RES | grep -v nameserver | cut -f 2 | grep name | cut -f 2 -d "=" | sed 's/ //'`; | |
fi | |
echo $NAME | |
done | |
# one-liner | |
$ for i in `cat urls.txt`; do nslookup $i 2>/dev/null | grep Address | tail -n 1 | cut -d " " -f 3; done > ips.txt | |
******************** |
nice! Been a while since I've looked at this gist, but glad to see it's still useful. :-)
@daretogo
You should put the $LOOKUP_RES
in double quotes, otherwise line breaks will not be kept and everything will end up as one line, hence tail -1
will grab the first occurrence which would be the nameserver rather than the response.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So if you want to go the other way - you have DNS names but you want IPs, I worked off yours and came up with:
Technically I only tested it on Mac, but should work the same on linux/bash.