Operating System - Linux
1753720 Members
4962 Online
108799 Solutions
New Discussion юеВ

Shell script: list all hosts in a network

 
SOLVED
Go to solution
Elif Gius
Valued Contributor

Shell script: list all hosts in a network

Hi
i want to discover which IPs are in used in a network.

I want to do something like:

nslookup 146.16.14.[1-254]

Any ideas?
4 REPLIES 4
Martin Brachtl
Advisor
Solution

Re: Shell script: list all hosts in a network

use ping with broadcast address.
Kasper Hedensted
Trusted Contributor

Re: Shell script: list all hosts in a network

Hi,

You could do this in a script:
i=0;while [ $i -le 254 ]; do nslookup 146.16.14.$i ; i=$((i+1));done
but it assumes that reverse lookup has been set for each IP-address on the DNS server.

Perhaps instead of nslookup, you could get a list from the DNS server ?

You could also tried and ping every ip-address in that segment, and see which ip-addresses that doesn't respond. But this assumes that the hosts are up and allowing icmp.

Cheers,
Simon Hargrave
Honored Contributor

Re: Shell script: list all hosts in a network

Your question confuses slightly.

If you want to find all IP addresses currently active on your subnet then do as martin says:

# ping 146.16.14.255

then every alive machine will respond.

However your question also mentions nslookup, so perhaps you want to query your DNS for all IP addresses that have names? If so then

i=1
while [ i -lt 255 ]; do
nslookup 146.16.14.$i | grep -i name
(( i = i + 1 ))
done
Elif Gius
Valued Contributor

Re: Shell script: list all hosts in a network

thanks all for the fast reply ...
Great!