I have a "headless" *NIX host -- let's call it 'foo' -- that is to be accessible via SSH and VNC-tunneled-over-SSH on an arbitrary 192.168.x.y/24 private LAN. Sometimes, I want to physically move 'foo' from one LAN to another, so 'x' depends on the LAN, and I want 'y' to be constant, with an ordered list of alternatives to handle allocation contention.

So although I want a static address ( to target with SSH ), I also need it to be dynamic ( to match the subnet of the LAN it has been attached to ).

Yes, some routers allow "special sauce" like DHCP reservation or additional routing configuration, but let's say that I don't have access to the router, but I can expect it to provide DHCP.

So, if I were to do this manually, I would set 'foo' up to use DHCP on boot, and once I get a shell, I can look at the gateway address to figure out my subnet, 192.168.x.0, where 'x' is what varies from one LAN to another; then I would edit '/etc/network/interfaces' ( or equivalent ) to switch from DHCP to static, synthesizing the address from 'x' and the constant 'y' ( first pinging 192.168.x.y to check if it's occupied and if so, choosing the next 'y' from a list of pre-defined choices, so in less optimal situations, I might have to try several addresses when I run SSH ), and then restarting the networking service.

A trickier variation would be a LAN that lacks DHCP; in that case, the subnet could probably be guessed by sampling some LAN traffic.

If such a setup could be automated, then 'foo' could simply be attached to an arbitrary LAN and booted, and there would be a good chance of finding its IP address first try.

Are there any clever ways to do what I'm after?

Remember, all configuration must be done on host 'foo', and the only assumption that can be made about the target LAN to which it is attached is that it uses a 192.168.x.y/24 private subnet.

  • you will have to leave it as DHCP. and run a script to analyze the gateway provided and then change the address. the tricky part will be making sure its set back to DHCP for the next boot, or the whole thing would call apart. The script itself would be OS dependent. its pretty easy to modify the /etc/network/interfaces file and restart the networking service on linux from a script, and windows has netsh and WMI that could do it. – Frank Thomas Apr 9 '15 at 18:36
  • Frank, right, this is *NIX-only, I've edited the question to reflect that. But yeah, it sounds like I have to cook up my own solution; I thought there might already exist a tool to do something like this. Is there a way to force '/etc/network/interfaces' to start from a "template" ( so it always boots with DHCP )...? – bernz Apr 9 '15 at 18:38
  • don't forget when you connect this ... um, shall we say "lurking" device ... into the ... ah, "foreign" network, your assumption of an IP range of 192.168 is inadequate. it could be 10, 10.1, 10.0.1, ... you get the drift. However it's no more difficult than your example. – Daniel Apr 9 '15 at 18:39
  • @bernz ... PM me if you want me to write this script for you. – Daniel Apr 9 '15 at 18:40
  • Thanks Daniel, agreed. In my use case, it's a RasPi that I want to drag between home and work -- not for anything nefarious, mind you ;-) -- and both networks are using 192.168.x.y private subnets. @Daniel, sorry, I'm a in novice mode here... there doesn't appear to be a PM feature for StackExchange... if you don't mind posting the script here, though, that'd be appreciated. :-) – bernz Apr 9 '15 at 18:41

Some bugs probably, but should be enough to do what's needed.

run as root.


LOCALNET=`route | grep default | awk '{print $2}' | cut -f1-3 -d'.'`
NETMASK=`route | grep default | awk '{print $3}'`
DEVICE=`route | grep default | awk '{print $8}'`

for ((xval=100; xval<254; xval++ )) ; do


    arping -c $ARPCOUNT $CURIP > /dev/null

    if ! [ $ARPRESULT ] ; then
    echo "No IP found at $CURIP."

    ifconfig $DEVICE down
    ifconfig $DEVICE $LOCALNET.$xval netmask $NETMASK
    ifconfig $DEVICE up
    echo "Changed to IP $CURIP."

  • Thanks, this looks like a great start; I will learn from and hack on it until it's working for my device. – bernz Apr 10 '15 at 12:56

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.