Finding All Hosts On the LAN From Linux / Windows Workstation

by Vivek Gite on March 5, 2008 · 27 comments

Q. How do I find out if all host computers on the LAN are alive or dead from a Linux or Windows XP computer? My network subnet range is 192.168.1.0/24 and I'm using dual boot Debian Linux / XP SP2 computer.

A.You can use normal ping command and shell script loop statement to print the list of all LAN computers from a shell prompt.

Linux / UNIX one liner to ping all hosts on the LAN

Type the following command, enter:
$ for ip in $(seq 1 254); do ping -c 1 192.168.1.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done
Output:

192.168.1.1 UP
192.168.1.1 UP
192.168.1.2 UP
192.168.1.5 UP
......
...
..
192.168.1.254 UP

See previous article: Simple Linux and UNIX system monitoring with ping command and scripts.

A Note About Windows Workstation

If you are using Windows 2000 / XP / Vista, try something as follows at DOS / NT command prompt (Start > Run > CMD > Enter key):
c:> for /L %I in (1,1,254) DO ping -w 30 -n1 192.168.1.%I | find "Reply"
Read cmd.exe help page and batch scripting documentation for more information.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 27 comments… read them below or add one }

1 Casper March 5, 2008

You would probably use
"for ip in $(perl -e '$,="\n"; print 0 .. 8;') ; do ping -c 1 192.168.1.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done"
on non GNU/Linux system, as seq does not exist on Solaris and OSX.

Reply

2 Casper March 5, 2008

Ups, this is nicer, and faster.
for ip in $(perl -e '$,="\n"; print 1 .. 254;') ; do ping -t 1 -c 1 192.168.1.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done

I’ve added a 1 sec. timeout on ping.

Reply

3 Ryan Sharp December 26, 2011

Looping in Perl just to print a sequence of numbers is really braindead.

Reply

4 richard March 5, 2008

Do you know fping ?
$ sudo apt-get install fping
$ fping -a -g 192.168.1.0/24 2> /dev/null

Reply

5 vivek March 5, 2008

@Casper,
Thanks for sharing perl only code.

@Richard,
Sure, fping was covered some time ago..

Reply

6 Scott Carlson March 6, 2008

I generally just use this “ping -b 192.168.1.255″ Which broadcasts a ping to the whole network at once.

Reply

7 Topper March 6, 2008

What about Windows machines with MS firewall which default forbid ICMP replay ?
Maybe must use ARP cache after ping

Reply

8 Z3n0 March 6, 2008

another way…

for ((ip=1;ip/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done

Reply

9 matthias March 6, 2008

If a host on your local network won’t answer on a ping request you could try arping, which does an arp request.
arping 192.168.1.1
ARPING 192.168.1.1 from 192.168.1.226 eth0
Unicast reply from 192.168.1.1 [00:01:02:xx:xx:xx] 0.668ms

Another method is simply using nmap:
nmap -sP 192.168.1.0/24

Reply

10 Z3n0 March 6, 2008

Something wrong with prev. comment… (< and > char)

for (( ip=1 ; ip<=254 ; ip++ )); do ping -c 1 -t 1 192.168.1.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.1.$ip UP" || : ; done

Reply

11 phillip March 7, 2008
12 sg March 7, 2008

another option is installing arpwatch.

Reply

13 suren March 16, 2008

wont this command do ???

nbtscan 10.0.0.1-125

this checks all the computers whose ip address are in the range of 10.0.0.1 to 10.0.0.125 and displays only those which are ON and connected to the network !

Reply

14 Johny May 6, 2009

Matthias, thumbs up for the nmap solution.

Reply

15 Remi Nodet June 4, 2009

Even faster (produces a little bit of confusing output in the beginning but it does the job fast):

for ip in $(perl -e '$,="\n"; print 1 .. 254;') ; do ping -t 1 -c 1 192.168.146.$ip > /dev/null && echo "192.168.146.$ip UP" >> hosts.log || : & sleep 0.02; done; sleep 1;cat hosts.log;rm hosts.log

Reply

16 jags January 8, 2012

this is awesome

Reply

17 nishu April 18, 2010

can anyone tell how to see the network address and system names in up in lan in linux..

Reply

18 hotsw4p July 24, 2010

i liked the nmap solution best as well, thanks Matthias for that.

Reply

19 mr August 25, 2010

check out the space after the n parameter: for /L %I in (1,1,254) DO ping -w 30 -n 1 192.168.1.%I | find “Reply”

Reply

20 horizons December 8, 2010

smbtree -SN |grep \\\\ |cut -f2 |cut -d”\\” -f3

will give a list of netbios host responding on broadcast address
(smbtree is part of the samba suite)

Reply

21 rst_ack February 9, 2011

#arp-scan -l

Reply

22 berner May 24, 2011

Very nice, all that. Thanks guys!

Reply

23 James June 20, 2011

Here’s a batchfile i made that pings any range of adresses:

@echo off & For /L %%i in (%4,1,255) do @ping -n 1 %1.%2.%3.%%i | find “Received = 0″ >nul & if errorlevel 1 @echo %1.%2.%3.%%i

most of the code is to tidy the output up. Save as PINGER.bat

Type:
PINGER 192 168 0 0
**without** the dots to find the range 192.168.0.0 to 192.168.0.255
or any other address PINGER 145 233 2 0 etc
cheers

Reply

24 lina July 13, 2011

How would you do this on a Mac? I tried it and got the error -bash: seq: command not found

Reply

25 Stan Tkhorovsky November 18, 2011

Thanks for ‘nast -m’ tip (3 years later ;) ) – liked it best.

@ lina – check out casper’s comment regarding non-Linux systems

Reply

26 Rodger January 2, 2012

Using cygwin on Windows, this did not work as expected.
It produced
192.168.1.1 UP

192.168.1.100 UP

for every IP address.

Problem is, this network is 192.168.2.1, not 192.168.1.1

Further investigation:
ping 192.168.2.1 produced a response from an IP from the ISP.

Know and Test what you are doing!

Reply

27 Lukas January 24, 2012

Hi, i cant find all hosts by nmap -sP 10.6.0.0/24
I know that router Mikrotik has got IP address 10.6.0.1, hi can i found with nmap ALL hosts at subnet? Which mode of nmap I have to use for this?
Could you help me, please? Thanks a lot.

Lukas

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 12 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the simple math so we know that you are a human and not a script.




Previous post:

Next post: