Created
May 15, 2023 20:20
-
-
Save iShariefi/8456956acbf6c801f7bca0cef05af8c8 to your computer and use it in GitHub Desktop.
A bash script to Reverse IP Lookup (a way to identify hostnames that have DNS (A) records associated with an IP address)
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/bash | |
stop=$# | |
inputIP="" | |
inputFile="" | |
knownIPFile="" | |
showUsage=0 | |
verbose=0 | |
for ((i=0; i<$stop; i++)) | |
do | |
if [ "${1}" == "-f" ]; then | |
if [ $((i+1)) -eq $stop ]; then | |
showUsage=1 | |
else | |
i=$((i+1)) | |
inputFile="${1}" | |
fi | |
elif [ "${1}" == "-k" ]; then | |
if [ $((i+1)) -eq $stop ]; then | |
showUsage=1 | |
else | |
i=$((i+1)) | |
knownIPFile="${1}" | |
fi | |
elif [ "${1}" == "-verbose" ]; then | |
verbose=1 | |
else | |
if [ $((i+1)) -eq $stop ]; then | |
inputIP="${1}" | |
else | |
showUsage=1 | |
fi | |
fi | |
shift | |
done | |
if [ $stop -eq 0 ]; then | |
showUsage=1 | |
fi | |
if [ $showUsage -eq 1 ]; then | |
echo "Usage: ip_lookup.sh [-multiline] [-k <KNOWN_IP_FILE] -f <FILENAME>" | |
echo " ip_lookup.sh [-multiline] [-k <KNOWN_IP_FILE] <IP_ADDRESS>" | |
echo "Bad Input" >&2 | |
exit 1 | |
fi | |
declare -A knownIPs | |
function LoadKnownIPs() { | |
# File should be IP<TAB>Description | |
while IFS=$'\t' read -r ip desc; do | |
knownIPs["$ip"]="$desc" | |
done < "$1" | |
} | |
if [ -n "$knownIPFile" ]; then | |
LoadKnownIPs "$knownIPFile" | |
fi | |
function LookupIP() { | |
result=$(nslookup "$1" 2> /dev/null | grep "Name:") | |
if [ -z "$result" ]; then | |
result="NOT FOUND" | |
else | |
result=$(echo "$result" | awk '{print $NF}') | |
fi | |
knownMatch="" | |
if [ -n "${knownIPs[$1]}" ]; then | |
knownMatch="${knownIPs[$1]}" | |
fi | |
if [ $verbose -eq 1 ]; then | |
echo "$1" | |
echo "$result" | |
echo "$knownMatch" | |
else | |
echo -e "$1\t$result\t$knownMatch" | |
fi | |
} | |
if [ -n "$inputFile" ]; then | |
while read -r line; do | |
LookupIP "${line}" | |
done < "$inputFile" | |
else | |
LookupIP "$inputIP" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment