Ok, so my question is a bit out there, and I've read a lot of similar questions but they never actually are doing what I am trying to accomplish.

So the best way to describe it is with this scenario:

I have 3 computers, all have Ethernet Adapters.

  • Desktop Computer - Windows, one I work from, have Cygwin.
  • A portable Raspberry Pi - One I use when I go to different places.
  • Laptop with Ubuntu - I take this just about everywhere as a backup plan

When I move my raspberry pi to another setup location, I need to be able to connect it directly to another computer via eth0. The issue is that I hate pulling out my laptop and enabling a bunch of forwarding solutions to allow this other persons machine to work with my pi.

I have to set the static IP of the Pi to something like 192.168.0.* and the work computer each time I want to connect to it. I want to stop having to pull out a keyboard and other tools to try to set these options. Now, I've already made a python script that sets the Pi's IP to as soon as it starts up and find its without a connection via DHCP.

This is good for connecting to my desktop that I'm forcing my Ethernet to stay at a certain IP, and have some hacks in the back for sharing connection interfaces. I just cant use this with other machines.

Which leads to the real question: How, if possible, can I get my Pi to somehow query the Ethernet to see if the computer its directly connected to already has a static IP address set, and then simply set its own to a matching IP to pair?

So what I'm saying is, I need to be able to just plug this pi into a machine. Which the IP for their adapter(the host) is already set to something. Say it's actually right now; and the pi will recognize 192.168.172._ and set it's address to something like 3 or 31 so I can SSH into it.

I've have been told by "professionals" that this cannot be done, but I don't believe it. This is the 21st century and we don't have mutative connections. Blasphemy. Haha, I want to figure this out and I think it will be useful to have this in my Linux arsenal. "The assimilate Ethernet daemon"

[EDITS:] As a clarification, the connection will be on a single cable. If it's in a network like a hub or switch, they have DHCP and the PCs will be given addresses automatically. So our solution only needs to be concerned with the random connection to another machine.

  • Zeroconf / Bonjour / avahi. But none of those is any use if the far end doesn't want to play. Do you have enough control of your endpoints to configure them to co-operate?
    – roaima
    May 26 '16 at 18:53
  • There are a lot of ways of finding the IP. The foolproof solution I would adopt is buying an LDP/hex display to the rpi and showing the IP there. May 27 '16 at 8:52
  • The idea was that I run into a host that doesn't want to be changed. So I can't mess with their IP settings, just see what they have. I need Pi to integrate itself within the parameters its host specifies; so, dealing with the static IP that the host chose and having to make its own something in the similar subnet so they can communicate. May 27 '16 at 15:40
  • your choices are active probing with arp-scan as in garethTheRed's answer, or passive monitoring with tcpdump or arp as in mine...just thought of another similar option - use arpwatch configured to email root@localhost when it spots a new IP/MAC combo, and a procmail rule to act on that mail.
    – cas
    May 27 '16 at 15:44
  • btw, you'll want some easy way to turn this off (maybe a semaphore file - your script should check if, say, /var/tmp/dont-change-the-ip exists and do nothing if it does. use touch and rm to turn on and off, maybe aliased as my-auto-ip-on and my-auto-ip-off) so that your r-Pi can be made safe to plug into existing networks - you don't want to do this on a network where there IS a dhcp server, for example.
    – cas
    May 27 '16 at 15:47

I don't want to put my foot in my mouth, but I don't think what you are trying to do is possible with any random machine you will connect this Pi to.

Think about it. The problem is two fold. At boot time your R-Pi has a fixed address. Right ? If the machine you are connecting to is not configured with that IP address/subnet mask, how are the two going to communicate and exchange information ? When you have your own laptop you are in a controlled environment. If you are connecting to a random machine, you have to expect the unexpected and then how are you going to handle it ? You can run a program to scan all possible IP addresses, starting with usual suspects as in 192.168.x.y or 172.10.x.y, but this is a very long and tedious process

Second thing is, if the machine you are connecting the R-Pi to is on a different network, say via WiFi or another network adapter, you are done again.

You need to understand, it is not your R-Pi controlling the connection. It is the other side. And when "other side" is random, what are you gonna do ?

  • You have a point, but that's why I'm asking, there must be a way to find out what the machine we are connecting to wants. If that machine is already set to a static IP, then it we need to know what it is somehow and adapt. Get it, then up the IP by 1. Logically we are connecting to another machine, using their Ethernet, most likely we aren't going to have a 3 way cable, so I just need to know what they are. May 26 '16 at 22:34
  • Consider this as well: If the IP address of the machine your are connecting your R-pi to is coming from a Wi-Fi connection, the hardware ethernet might not even be turned on and assigned an address to. Another downfall in the plan. Are you sure all machines you are connection your gizmo to, have an active harware network interface with IP address and everything ? If not, you are dead in water already as you can not force a foreign machine to change network configuration, by connecting to the the said network interface.
    – MelBurslan
    May 27 '16 at 18:42

You can use arp-scan:

$ arp-scan --interface=eth0

or nmap:

$ nmap -sP

to scan all addresses on a network for a response.

The problem is: do you assume that every computer your Pi connects to is on the network, as the examples above show, or not?

If you do, then a scan won't take long - 3.56 seconds with nmap and 2.28 seconds with arp-scan on my WiFi network.

Do you assume that all are on the full as specified in RFC 1918 which takes about 4.5 minutes with arp-scan?

At the same scanning speed, a scan of would take nearly 18 hours.

But if you scan the full 4,294,967,296 possible IP addresses it could take over half a year!

Then, of course, we're assuming it has an IPv4 address. It might be on IPv6, which works out at 2,420,352,024 years!

If you scan anything bigger than a you might as well plug in a keyboard :-)

  • Yeah, brute force is something I was hoping to avoid. :\ May 26 '16 at 22:50

If you plug your r-pi into a network that has a reasonable amount of traffic on it, you can monitor ARP traffic on the interface.

This is similar to @garethTheRed's answer, but relies on passive monitoring rather than active probing. It may take a significant amount of time before an ARP packet is seen. Running ping or something from the machine you want to connect from will help.

You could use tcpdump -l -n -i eth0 arp to listen on eth0 for ARP packets. When your script spots an ARP who-has or ARP Reply packet, extract the IP address, pick a random IP in the same subnet that isn't already in the ARP table, and try using it.

Alternatively, instead of tcpdump, you could run arp -n -i eth0 | awk 'NR>1 && !/incomplete/ {print $1}' every so often to output a list of seen IP addresses from the kernel's ARP table.

WARNING: The fact that you haven't seen an IP being used on the network while you were watching does not mean that it is not being used by or allocated to another machine. It might mean that it just hasn't been active on the network recently, or that you're on a switched network and it hasn't sent any broadcast packets. Expect to get yelled at if you hijack an IP address belonging to someone else on the network. There may be further consequences depending on the side-effects of hijacking the IP (e.g. hijacking the IP address belonging to a server or router, or the boss's laptop, could be a sackable offence)

Another option that will probably be a lot less hassle is to:

  1. set a static IP address on your r-PI. Use a private RFC-1918 IP address. Use the same subnet for all computers on your own LAN. Pick one that's unlikely to be in use at other sites (i.e. NOT one of the commonly used defaults like 192.168.0.x).

  2. whenever you need to communicate with your r-PI from another machine, just add a second IP address in your subnet to that machine's ethernet interface if it's not already on the same subnet.

Note: network switches are sometimes configured to drop packets from unknown subnets. In that case, your best option is to just give in and have your r-PI be a DHCP client (do not run a dhcp server on your r-PI and then plug it into a network you don't own/control or which has another dhcp server running on it).

  • Yeah, we don't have to worry about a network. Networks usually already have dhcp servers that will make sure the computer gets an IP, and in that case, the Pi will take it and not worry about all of this. Our problem lies within the communication between two machines without a middleman. Also, I did specify that sometimes I would not have access to the host machines IP settings. They may want to set it to something themselves, my Pi has to be flexible if I run into someone who's stubborn. May 27 '16 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.