<h1>Locating computers on a network</h1>

<p>Sometimes you may wish to locate a device on your network. It might be to find rogue devices, or to locate a new device be that a printer, an Internet of Things light bulb, or whatever. In my case I was trying to track down a RaspberryPi. Raspbian now lists the IP Addresses assigned to it when it first starts, but that requires a monitor to be connected.</p>

<h2 id="locating-using-your-router">Locating using your router.</h2>

<p>This is by far the easiest way to find a new device but it is also the most manual. If you have access to the (WiFi) Router/<a href="https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol">DHCP</a> server on your network then this should be a matter of logging in and getting the address from the menu area probably called <code>Network</code> or <code>LAN</code> or something like that. So first list all the computers that are on the network. Then turn on your pi, and see if a new one has joined. If you're lucky it may even be labeled <em>Raspberry Pi Foundation</em>.</p>

<p>In my case I want to locate these devices automatically using a script. So below we will explore some ways that you can approach the problem of locating devices on your network. All of them rely on trying to find the mapping between an Ethernet MAC Address, and a IP Address. It will help to understand a little about how Ethernet works. This is a nice short overview, <a href="https://www.youtube.com/watch?v=5u52wbqBgEY">How does Ethernet work? (animated)</a> by <a href="https://www.youtube.com/channel/UCXkx8hX8Xo4lb7T0_-yZzVg">Janos Pasztor</a></p>

<h2 id="arp">ARP</h2>

<p>I don't know the network device you will be using so I'd like to share a few ways of doing this using the <a href="https://en.wikipedia.org/wiki/Address_Resolution_Protocol">ARP</a> protocol. This provides a mapping between a device's permanent <a href="https://en.wikipedia.org/wiki/MAC_address">MAC address</a> and their, often temporarily, assigned <a href="https://en.wikipedia.org/wiki/IP_address">IP address</a>.</p>

<p>When your computer <code>192.168.1.1</code> wants to talk to another device it will need to know the Ethernet <a href="https://en.wikipedia.org/wiki/MAC_address">MAC address</a> of the other device. Let's assume it wants to connect to the local printer <code>192.168.1.2</code> that is on the same network, and it knows its IP Address. If your computer has already heard the printer communicate on the network, it will have already added the printer's IP address and MAC Address to its so called ARP table. Over time older entries will be removed from this table either because they are too old, or there were too many entries.</p>

<p>Linux has a command called <code>arp</code> which will show you a list of the IP addresses mapped to the MAC/HWaddress address.</p>
<pre><code># arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.1.254            ether   ff:ee:dd:cc:bb:aa   C                     eno1
192.168.1.2              ether   00:11:22:33:44:55   C                     eno1
192.168.1.1              ether   00:aa:bb:cc:dd:ee   C                     eno1
</code></pre>

<p>From a <a href="https://en.wikipedia.org/wiki/Internet_Protocol">Internet Protocol (IP)</a> point of view all your devices in your house are probably in the same <a href="https://en.wikipedia.org/wiki/Subnetwork">Subnet(work)</a>. But what if you need to communicate outside your network? For example to ping a well known server <code>8.8.8.8</code> to check if you have an Internet Connection. The IP settings also include a <a href="https://en.wikipedia.org/wiki/Default_gateway">Default gateway</a> to send all traffic not intended for the local network.</p>

<h2 id="finding-the-default-gateway">Finding the Default gateway</h2>

<p>Your computer will have many IP Addresses configured but it's a good bet that the network you and your new pi are on is the same one as the default gateway is on. You can find the default gateway using the following command:</p>
<pre><code>$ route -n | grep -E 'Destination|^0.0.0.0'
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.254   0.0.0.0         UG    100    0        0 eno1
</code></pre>

<p>It uses <code>route</code> to show the route table showing IP Addresses and not network names. The first column is <code>Destination</code> and so a <code>0.0.0.0</code> there means <em>route everything else to</em> and the <em>to</em> we get from the second column <code>Gateway</code>, which is our gateway.</p>

<p>You can also get the same information using the following command:</p>
<pre><code>$ ip route show | grep default
default via 192.168.1.254 dev eno1 proto dhcp metric 100
</code></pre>

<p>And now to just show the IP Address.</p>

<pre><code>$ route -n | awk &#39;/^0.0.0.0/ {print $2}&#39;
192.168.1.254
</code></pre>


<p>From an Ethernet point of view it needs to find the <a href="https://en.wikipedia.org/wiki/MAC_address">MAC address</a> associated with your router/default gateway's IP address <code>192.168.1.254</code>, so that it can send Ethernet Frames to it. In our case the address is in the ARP table, but if it wasn't then your computer would broadcast to everyone on the network using a special address <code>ff:ff:ff:ff:ff:ff</code>. When the router/default gateway replies that entry will be added to the ARP table.</p>

<pre><code>
739  5.328679069  00:aa:bb:cc:dd:ee  ff:ff:ff:ff:ff:ff  ARP  42  Who has 192.168.1.254? Tell 192.168.1.1
746  5.328911559  ff:ee:dd:cc:bb:aa  00:aa:bb:cc:dd:ee  ARP  60  192.168.1.254 is at ff:ee:dd:cc:bb:aa
</code></pre>

<p>Have a play with <a href="https://www.wireshark.org/">WireShark</a> to see what is actually going on on your network. There is a good step by step walk through on <a href="https://en.wikiversity.org/wiki/Wireshark/Arp">Wireshark/Arp</a> on wikiversity.</p>

<p>All RaspberryPi computers have an Ethernet <a href="https://en.wikipedia.org/wiki/MAC_address">MAC address</a> starting with <a href="https://hwaddress.com/company/raspberry-pi-foundation"><code>b8:27:eb</code> or <code>dc:a6:32</code></a>. So if your computer and the new pi are on the same network you can look for new MAC addresses starting with <code>b8:27:eb</code> or <code>dc:a6:32</code>.</p>

<h2 id="foiled-by-the-switch">Foiled by the Switch</h2>

<p>All is not plain sailing as most modern <a href="https://en.wikipedia.org/wiki/Local_area_network">LAN</a>'s use <a href="https://en.wikipedia.org/wiki/Network_switch">switches</a> to split up the <a href="https://en.wikipedia.org/wiki/Broadcast_domain#Further_explanation">broadcast domain</a>.</p>

<blockquote cite="https://en.wikipedia.org/wiki/Broadcast_domain#Further_explanation">
Switches act as buffers, receiving and analyzing the frames from each connected network segment. Frames destined for nodes connected to the originating segment are not forwarded by the switch. Frames destined for a specific node on a different segment are sent only to that segment. Only broadcast frames are forwarded to all other segments. This reduces unnecessary traffic and collisions.
</blockquote>

<p>What this means is that although all your computers may be on one big IP network, the devices may be connected to different switches. For example a WiFi Router downstairs, a Cabled Switch in the back room, and a network extender in the attic. All are filtering out unnecessary traffic. While that is a good thing, it does mean that it may not ever see the traffic from the new Pi.</p>

<p>If we try and contact each and every IP Address on the network, that forces ARP "Who has?" traffic, and the answers that come back will be added to the arp table. There are a few ways to force this discovery.</p>

<h2 id="discovery-by-ping">Discovery by ping</h2>

<p>One way to generate ARP traffic is to use the <a href="https://en.wikipedia.org/wiki/Ping_(networking_utility)">ping</a> utility.</p>

<blockquote cite="https://en.wikipedia.org/wiki/Ping_(networking_utility)n">
Ping measures the round-trip time for messages sent from the originating host to a destination computer that are echoed back to the source. The name comes from active sonar terminology that sends a pulse of sound and listens for the echo to detect objects under water.
</blockquote>

<p>To ping an IP Address, just type <code>ping 192.168.1.1</code>. That will send out a continuous ping which you can stop by pressing and holding the <em>ctrl</em> key and then pressing the letter <em>c</em>.</p>

<pre><code>$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=0.547 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=63 time=0.493 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=63 time=0.422 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=63 time=0.466 ms
^C
--- 192.168.1.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3105ms
rtt min/avg/max/mdev = 0.422/0.482/0.547/0.045 m
</code></pre>

<p>We don't actually care about the output, as the point here is to generate some traffic to see who is awake. So we can redirect output and errors to <a href="https://en.wikipedia.org/wiki/Null_device">/dev/null</a>. So a good command to do this would be:</p>

<pre><code>ping -n -c 1 -W 1 192.168.1.1 &gt;/dev/null 2&gt;&amp;1 &amp;</code></pre>

<p>The options used are to limit the traffic and wait time as much as possible:</p>

<pre><code>-n Numeric output only.  No attempt will be made to lookup symbolic names for host addresses.
-c count. Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.
-W timeout. Time to wait for a response, in seconds. The option affects only timeout in absence of any responses, otherwise ping waits for two RTTs.
</code></pre>

<p>In this solution we will try and <a href="https://en.wikipedia.org/wiki/Ping_(networking_utility)">ping</a> every host on the network. It would be a bit tedious to do each one by one, so we will make a script to do this for us. It's a very safe bet that your home network will have a private IP range of 192.168.???.0 to 192.168.???.255. This is not a safe assumption in an Office Environment but we can deal with that later.</p>

<p>So we will try and find the <em>base</em> part of the subnet <code>192.168.1.1</code> which will be <code>192.168.1</code> and then we will ping each address in turn.</p>

<pre><code>base=$( ip route show | awk -F &#39;\\.|/| &#39; &#39;/default/ {print $3&quot;.&quot;$4&quot;.&quot;$5}&#39; )
for node in {1..254}
do
( ping -n -c 1 -W 1 ${base}.${node} &amp; )
done
</code></pre>

<p>I'm assigning the <code>base</code> variable the first three parts of the IP Address and then I am cycling through all possible addresses sending out a <a href="https://en.wikipedia.org/wiki/Ping%20%28networking_utility%29">ping</a> (a sort of hello message) to each. By wrapping the whole thing in <code>()</code> we can <em>fork</em> each to run at the same time.</p>

<p>This will force some traffic between my computer and every computer on the network, which will fill up the arp table on my pc.</p>

<pre><code># arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.1.2              ether   00:11:22:33:44:55   C                     eno1
192.168.1.1              ether   00:aa:bb:cc:dd:ee   C                     eno1
192.168.1.10             ether   11:11:11:11:11:11   C                     eno1
192.168.1.25             ether   22:22:22:22:22:22   C                     eno1
192.168.1.3              ether   33:22:22:22:22:22   C                     eno1
192.168.1.5              ether   b8:27:eb:11:11:11   C                     eno1
</code></pre>

<p>And now we find all the computers that are on, and responding to pings on the network. We can see that there is one that starts with <code>b8:27:eb|dc:a6:32</code>, and it has an IP Address of 192.168.1.5.</p>

<pre><code># arp -n | grep -Ei &#39;b8:27:eb|dc:a6:32&#39; | awk &#39;{print $1}&#39;
192.168.1.5
</code></pre>

<p>Putting it all together</p>

<pre><code>base=$( ip route show | awk -F &#39;\\.|/| &#39; &#39;/default/ {print $3&quot;.&quot;$4&quot;.&quot;$5}&#39; )
for node in {1..254}
do
  (ping -n -c 1 -W 1 ${base}.${node} &gt;/dev/null 2&gt;&amp;1 &amp;)
done
arp -n | grep -Ei &#39;b8:27:eb|dc:a6:32&#39; | awk &#39;{print $1}&#39;
</code></pre>

<h2 id="discovery-by-nmap">Discovery by nmap</h2>

<p>You should have <strong>permission in writing</strong> from your network administrator before you proceed with these steps.</p>

<p>Here I am going to use some of the commands above together with the tools <a href="https://nmap.org/">nmap</a> and <a href="https://github.com/nmav/ipcalc">ipcalc</a> to locate and scan the network. This has the advantage of dealing with more exotic subnet masks.</p>

<p>Find the default interface</p>

<pre><code># route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.254   0.0.0.0         UG    100    0        0 eno1
....

# route -n | grep -E &#39;^0.0.0.0&#39; | awk &#39;{print $8}&#39;
eno1
</code></pre>

<p>Find the ip and subnetmask for the default interface</p>
<pre><code># ifconfig eno1 | grep &#39;inet &#39;
inet 192.168.1.1  netmask 255.255.255.0  broadcast 192.168.1.255

# ifconfig eno1 | grep &#39;inet &#39; | awk &#39;{print $2&quot;/&quot;$4}&#39;
192.168.1.1/255.255.255.0
</code></pre>

<p>Get the network from ipcalc</p>

<pre><code># ipcalc 192.168.1.1/255.255.255.0
Address:        192.168.1.1
Network:        192.168.1.0/24
Netmask:        255.255.255.0 = 24
Broadcast:      192.168.1.255

Address space:  Private Use
Address class:  Class C
HostMin:        192.168.1.1
HostMax:        192.168.1.254
Hosts/Net:      254

# ipcalc 192.168.1.4/255.255.255.0 | grep &#39;Network&#39; | awk &#39;{print $NF}&#39;
192.168.1.0/24
</code></pre>
<p>Now call nmap to give the ip address and MAC address</p>
<pre><code># nmap -sn 192.168.1.0/24
Starting Nmap 7.40 ( https://nmap.org ) at 2017-07-05 20:57 CEST
Nmap scan report for 192.168.1.2
Host is up (0.0012s latency).
MAC Address: 00:11:22:33:44:55 (Acme industries)
Nmap scan report for 192.168.1.5
MAC Address: b8:27:eb:11:11:11 (Raspberry Pi Foundation)
Nmap scan report for local.lan (192.168.1.254)
Host is up.
Nmap done: 256 IP addresses (2 hosts up) scanned in 3.51 seconds


-sn (No port scan) This option tells Nmap not to do a port scan after host discovery, and only print out the available hosts that responded to the host discovery probes. This is often known as a “ping scan”, but you can also request that traceroute and NSE host scripts be run. In previous releases of Nmap, -sn was known as -sP.
</code></pre>

<h2 id="discovery-by-arp-scan">Discovery by arp-scan</h2>

<p>By far the easiest way to do this is using the dedicated tool called <a href="https://github.com/royhills/arp-scan">arp-scan</a>, (<a href="https://linux.die.net/man/1/arp-scan">Man Page</a>)</p>

<pre><code># arp-scan --interface eno1 --localnet --numeric --ignoredups
Interface: eno1, type: EN10MB, MAC: 20:47:47:ca:24:a6, IPv4: 192.168.10.14
Starting arp-scan 1.9.7 with 256 hosts (https://github.com/royhills/arp-scan)
192.168.1.2     00:11:22:33:44:55       Acme industries
192.168.1.5     dc:a6:32:22:22:22       Raspberry Pi Trading Ltd
192.168.1.233   b8:27:eb:11:11:11       Raspberry Pi Foundation
192.168.1.254   e4:95:6e:00:00:00       IEEE Registration Authority

12 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.9.7: 256 hosts scanned in 1.939 seconds (132.03 hosts/sec). 12 responded
</code></pre>

<p>Whichever option you chose I hope this helps you become more familiar with your network and the way it works.</p>
