Piduna
Piduna

Reputation: 175

Searching by nslookup command my own ip address from many others ip

i have a lot of virtual servers with static ip-addresses, and with A-records for them. I should use associative array for installing of my CMS. But i have idea. Maybe i should use dig, or other dnsutil for that ? So, what i start write:

start=test
dns=com
for i in "${start}" {1..20}."$dns"; do
echo $i >> "/tmp/temp"
done

for ns in `cat /tmp/temp`; do
if [[ `dig +short $ns=="192.168.110.1"` ]]; then
dig +short $ns
fi
done

But with my second loop something wrong. Can you help me ? I should generate a list with my domains, like test1.com, test2.com ... And after that i should get ip address. The next step will comparing with my system ip and if i have ip 192.168.110.1, i should get my domain name, like test2.com. It does not work, i broke my head, but i have no idea, how to do this. Please help, if it possible.

Upvotes: 1

Views: 298

Answers (1)

tripleee
tripleee

Reputation: 189317

The immediate error is that [[ `dig +short $ns=="192.168.110.1"` ]] simply checks whether the output from dig is a nonempty string (which it isn't, because the string you pass in as a query isn't a valid one). The superficial fix is

if [[ `dig +short "$ns"` == "192.168.110.1" ]]; then ...

where the spaces around the equals operator are significant, and of course, the comparison should not be passed to dig as an argument; but I would refactor your script a whole lot more. It's not entirely clear what exactly you want the script to do, but something like this?

#!/bin/bash
start=test
dns=com
for i in {1..20}; do
    host="$start.$i.$dns"
    ip=$(dig +short "$host")
    if [[ "$ip" == "192.168.110.1" ]]; then
        # I'm guessing you want to perform a reverse lookup on the IP address here?
        dig +short "$ip"
    fi
done

Upvotes: 1

Related Questions