How can I list all IPs in the connected network, through Terminal preferably?

228

111

Using Ubuntu 10.10, I was wondering if there was a command line command that can list all the IPs of devices connected to the network?

I would use this for example, to list all the computers connected to my home network. Ideally, it needs to be command line as I will be running it from C++.

Any ideas?

Christopher Gwilliams

Posted 2011-03-24T13:27:13.423

Reputation: 2 435

Answers

247

Check out the arp-scan command - you will probably have to install it eg:

sudo apt-get install arp-scan

http://manpages.ubuntu.com/manpages/hardy/man1/arp-scan.1.html

And to give further detail:

sudo arp-scan --interface=eth0 --localnet

Where eth0 is your device. You can find your device with:

ifconfig

Linker3000

Posted 2011-03-24T13:27:13.423

Reputation: 25 670

2It's worth noting that this does not use the ARP cache: it performs an actual scan. Unlike most nmap scans, this cannot cross layer 3 boundaries, which is usually what you want in this scenario. (In other words, it's limited to your subnet.) – Zenexer – 2014-09-29T12:26:37.670

On newer versions, it has become p1p1 instead of eth0 – Grammargeek – 2016-03-25T12:05:11.893

what is the difference comparing with nmap? – dspjm – 2017-02-07T00:23:42.087

On Debian Jessie, this does not work if the computer from which you are issuing the command is connected via wifi to the network. nmap, on the other hand, works in both cases: cable and wifi. – Federico – 2017-06-11T02:25:02.403

works on CentOS 7. – Jeff – 2017-10-11T00:24:13.053

What For windows? – pyd – 2017-11-12T16:11:50.707

Perfect, thank you. Returns exactly the format I need. – Christopher Gwilliams – 2011-03-24T14:09:15.070

1Works perfectly on OSX too! Installed with Brew, and interface was en0, rather than eth0, but works great. – nthonygreen – 2013-04-13T16:00:41.393

6If your connected to your network using wifi use wlan0 instead of eth0. – Neil – 2013-09-15T12:04:53.813

3Can you get this to list hostnames as well as IP addresses? – user1527227 – 2014-02-04T18:57:06.130

115

Use nmap. example: nmap -sn 10.10.10.0/24 The arp cache will only tell you those that you have tried to contact recently.

Keith

Posted 2011-03-24T13:27:13.423

Reputation: 7 263

2is 10.10.10.0 my given IP? What is 24 in this case? Thanks. – kolonel – 2015-04-20T07:15:16.310

4@kolonel That's just an example. You should substitute it with your network. The 24 is "slash notation" of the subnet mask. It means use 24 bits from the left. It's equivalent to 255.255.255.0. – Keith – 2015-04-25T17:05:33.923

2@Keith do you know how I'd find out my network address to use? Or is it just my IP with 0/24 at the end? – TMH – 2016-01-25T11:01:47.060

1

@kolonel:https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

– arkod – 2016-02-10T19:56:57.353

He is right. "arp" only shows ip addresses which we tried to contact recently. I cleared arp cache and again listed "arp -a" and found nothing. If in case anyone want to clear arp cache use this command "ip -s -s neigh flush all". – learner – 2019-12-08T14:37:43.137

20

In windows this would be "arp -a" I believe that an equivalent of that in Linux would be "arp -e".

This information can be found from the man page for arp :

arp with no mode specifier will print the current content of the table.
-e : Use default Linux style output format (with fixed columns).

David

Posted 2011-03-24T13:27:13.423

Reputation: 6 975

Both work on Linux, but arp -a displays (all) hosts in alternative (BSD) style. Simpy running arp does the same as running arp -e beacuse that's the default. – simplegamer – 2016-09-02T20:48:25.373

That looks like it works (may need to have a play as I am on a public network so it only seems to list all the servers on the network. – Christopher Gwilliams – 2011-03-24T13:37:25.067

If I am on a home network, will it just list the devices connected to my router? Thanks! – Christopher Gwilliams – 2011-03-24T13:37:51.480

1That command will only list the devices in the hosts current arp cache and that will only be the ones which the host has contacted recently. – Linker3000 – 2011-03-24T13:38:16.720

16

If your network is 192.168.0.0/24, make an executable file with the following code; Change the 192.168.0 to your actual network.

#!/bin/bash
for ip in 192.168.0.{1..254}; do
  ping -c 1 -W 1 $ip | grep "64 bytes" &
done

Anders Larsson

Posted 2011-03-24T13:27:13.423

Reputation: 161

@NevinWilliams (required -b, removed -W to wait more) ping -b -c 1 192.168.0.255 result "1 packets transmitted, 0 received, 100% packet loss, time 0ms" – Aquarius Power – 2016-10-31T21:17:51.507

Hi Anders... The user's network might not be 192.168.0.0/24; I made a note. The site's formatting did not like a bare #!, and so clobbered the formatting of the code snippet: when entering code, use the blockquote or preformatted text buttons, and review your answer for proper formatting before submitting it, as carriage returns, tabs, and spaces might have been stripped. – Nevin Williams – 2013-05-17T19:34:47.593

As well, when supplying a script that needs to be put into a file and made executable, rather than simply cut & pasted, it's probably best that you specify this; it may not be obvious to some what all is required to implement your solution. – Nevin Williams – 2013-05-17T19:37:01.427

13Technically, this will only return hosts that respond to ping. There may be hosts that are connected, but not replying to ICMP echo requests. As well, one can broadcast one packet to an entire network by specifying the broadcast address, which is the last address in the IP network: ping -c 1 -W 1 192.168.0.255 would accomplish the same as the for loop. – Nevin Williams – 2013-05-17T19:45:31.847

13

Try installing nmap (sudo apt-get install nmap) and type nmap 192.168.1.0/24 substituting 192.168.1 with the first three parts of your ip address (find out using ip addr).

You can also get a slightly less accurate (in my experience) map of a network by running ping 192.168.1.255 (again substituting 192.168.1), which should issue a ping to every machine on the network, but, in my experience, does not always function correctly.

Samadi

Posted 2011-03-24T13:27:13.423

Reputation: 241

Actually, none of the answers will always work correctly. IP wasn't designed with this requirement in mind, and there are things like Private VLANs which make it impossible to find any other hosts on the same LAN. – Ron Maupin – 2016-04-09T23:22:31.733

3

Came up with the following on a nexus using tmux as arp-scan isn't in the repo but nmap came pre-installed, displays just the ip addresses:

nmap -sn 192.168.1.1-254/24 | egrep "scan report" | awk '{print $5}'

zentek

Posted 2011-03-24T13:27:13.423

Reputation: 31

1

For a more compact list of connected devices:

nmap -sL 192.168.0.* | grep \(1

Explanation.

nmap -sL 192.168.0.* will list all IPs in subnetwork and mark those, that have name:

Nmap scan report for 192.168.0.0
Nmap scan report for Dlink-Router.Dlink (192.168.0.1)
Nmap scan report for 192.168.0.2
...
Nmap scan report for android-473e80f183648322.Dlink (192.168.0.53)
...
Nmap scan report for 192.168.0.255

As all interesting records start with parenthesis ( and digit 1, we filter for that with | grep \(1 (backslash is needed to escape parenthesis)

Quirk
Beware that if two devices have the same name, nmap will show only the one, that was connected to router last

Alexander Malakhov

Posted 2011-03-24T13:27:13.423

Reputation: 131

If the you have the exact same answer for two questions then it may be worth considering flagging the questions as duplicate rather than posting duplicate answers. That way knowledge can be shared as similar questions get linked together. – Mokubai – 2016-10-22T07:02:58.070

1@Mokubai You are right, I've add comment to the OP. This shows an interesting problem. A quick search discovered 6 duplicates (this Q and 5 links) across 4 network sites (SU, Ask Ubuntu, SF, Unix). Sure there are much more! How do I handle this ? Ideally, each of these 6 posts should link to 5 others. Adding all these links by hand clearly doesn't scale. So, for now I've linked to this post (most upvoted). Another problem is that it's impossible to mark Q on AskUbuntu as a duplicate of the Q on SU. Hm... Probably, this was already discussed on the meta ? – Alexander Malakhov – 2016-10-22T09:35:15.030

doesn't work for me on Centos 7, just lists every possible IP address in the network with no names. arp-scan worked for me. – Jeff – 2017-10-11T00:20:33.473

0

Ellaborating on Anders Larrson's answer -

#!/bin/bash
function scan ()
{
    for ip in $1.{1..254}; do
        ping -c 1 -W 1 $ip &
    done | sed -nE 's:^.* from ([0-9.]+).*time=(.*s)$:\1 (\2):p'
}

if [ $1 ]; then
    for baseip; do
        scan $baseip
    done
else
    scan 192.168.1
fi

Mathieu CAROFF

Posted 2011-03-24T13:27:13.423

Reputation: 101