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.