nslookup-scan of IP-range/subnet

#!/bin/bash
# nslookup-scan of IP-range

# It's possible to add more networks separated with space
NETS="192.168.0"

IPRange="1 254"
for NET in $NETS; do
  for n in $(seq $IPRange); do
        ADDR=${NET}.${n}
        echo "${ADDR},`nslookup ${ADDR} | awk -F "=" '{ print $2 }'|sed 's/^[ t]*//' | sed '/^$/d' | |sed 's/.$//'`"
  done
done

Result

192.168.0.1,cba.infra.no
192.168.0.2,bca.infra.no
192.168.0.3,abc.infra.no
192.168.0.4,
192.168.0.5,
192.168.0.6,
192.168.0.7,
192.168.0.8,

3 thoughts on “nslookup-scan of IP-range/subnet

  1. #!/bin/bash
    # nsscan.sh
    # nslookup-scan of IP-range

    # Get subnets from arguments
    NETS=$@

    # Handle lack of arguments
    if [ “x$NETS” = “x” ]; then
    echo “Usage example: sh nsscan.sh 192.168.1”
    echo “This would scan 192.168.1.0/24”
    echo “Accepts class C subnets only”
    exit 2
    fi

    IPRange=”1 254″
    for NET in $NETS; do
    for n in $(seq $IPRange); do
    ADDR=${NET}.${n}
    # Only print line if nslookup returns a result
    if [ “x$(nslookup ${ADDR} | awk -F ‘=’ ‘{ print $2 }’| sed ‘s/^[ t]*//’ | sed ‘/^$/d’ | sed ‘s/.$//’)” != “x” ]; then
    echo “${ADDR} `nslookup ${ADDR} | awk -F “=” ‘{ print $2 }’|sed ‘s/^[ t]*//’ | sed ‘/^$/d’ | sed ‘s/.$//’`”
    fi
    done
    done

  2. There’s an error in the regular expression :
    you must type
    sed ‘s/^[[:blank:]]*//’
    instead of
    sed ‘s/^[ t]*//’

    And there’s also a typo error :
    you must type
    | sed ‘s/.$//’
    instead of
    | |sed ‘s/.$//’

Comments are closed.