1

I work on a Linux system with "classical" network setup, i.e. there is no full featured configuration daemon running like NetworkManager. Most configuration is done by dhclient.

I need to detect situations where I have to re-run dhclient. (different subnet due to cable or network config changes). I want to run the check every 10 minutes without cutting existing connections.

What I basically need is an answer to questions like Is my IP Adress and Gateway valid for the Network I am connected to? or If I would renew my DHCP Lease, would I gett the same IP again?. To answer the first question, one could just ping some public IP like 8.8.8.8, but I need a solution that also works in big private networks I have no knowledge about. Even the default gateway might not always be pingable.

One I knew my ip is invalid I would do "dhclient -r eth0 ; dhclient eth0" to renew the lease. However, this disconnects me from internet for a short time, if I am already connected properly.

I found out that dhcping is a good tool to ask the DHCP server without actually using the result. However, I get strange results, which may even qualify for another question. It seems that dhcping only works well if I know the servers ip in advance, so there is a tool missing for the DHCP DISCOVER. (and using broadcast server adresses is unreliable)

I know that I can do hdcp discover with nmap, however, installing nmap is not a good idea due to security reasons.

Questions I'd like to ask:

  • Is there a way to validate my configured ip in the context of the connected network?
  • Is there a chance to have the dhclient lease renewed on demand?
  • Are there tools doing a dhcp discover broadcast and printing the result?

Addendum:

Unlike the results of my first test, just calling dhclient -v does not solve my problem. To clarify this, my initial problem is dhclient running in a background mode (without actually renewing the lease all the time). Calling dhclient results in:

dhclient(30887) is already running - exiting.

This is not fully repoducable, though. Sometimes it succeeds. (This is why I first thought the -v option would make a difference, it does not)

So my question is: If I get this already running issue, although dclient is currently doing nothing, how to trigger a renewal for the background process?

(Rationale behind this is, that a DHCP client needs to bind to port 68. When this port is bound by the dhclient background daemon, another instance cannot be executed in foreground)

2

The answer to the first two questions is the same: use

dhclient -v eth0

(-v stands for verbose, not all Linux distros have it), instead of dhclient -r eth0; dhclient eth0. This shorter form first asks for a re-assignment of the current IP address, and then starts a negotiation with the router only if this cannot assign the requested IP address, either because it is taken already or because it is outside the gateway's DHCP range.

I suggest you use the -v flag so that you can see this exchange, it is quite explicit.

As for your third point, there is AFAIK no dry-run option for any dhcp client. If you really insist on that, however, you can do the following: set up a macvlan interface, and start a dhcp instance for this new interface:

ip addr flush dev eth0
ip link add link eth0 mac0 type macvlan mode bridge
dhclient -v mac0

At this point, you can see the configuration provided by the local DHCP server, network, netmask, gateway and DNS servers on the new, virtual interface. When you are done,

ip link del dev mac0
dhclient -v eth0

will restore exactly the configuration you had before, if the DHCP server can dish out your old IP address. Otherwise, you are stuck with a new one ;-) . All of this is trivial to script, really.

And, BTW, if your problem is the persistence of ssh connections, I suggest you check mosh instead.

| improve this answer | |
  • Thanks for this great and comprehensive answer! Its a bit confusing that dhclient changes behaviour just by adding the -v flag, but this is exactly what I need. And btw, it is not about ssh, but I'm glad you mentioned mosh. This seems to be an interesting project. – philipp Aug 29 '16 at 7:54
  • @philipp Sorry for leading you into error : dhclient changes behavior when you drop the -r flag; the -v flag only displays the negotiation. When you do dhclient -r eth0,; dhclient eth0 the second invocation cannot ask for a lease of its old IP address, because you have just relinquished it. – MariusMatutiae Aug 29 '16 at 8:22
  • Thanks: indeed there is no difference, and this is still not the solution for my problem. I still get the "already running" message most of the time, which is my root problem. I added this to my question – philipp Aug 29 '16 at 12:12
0

Best solution I found so far is using a dhcp discover implementation in python:

https://code.activestate.com/recipes/577649-dhcp-query/

Which is quite nice, as I already use python. But maybe I am still missing a simple solution and this is all overcomplicated.

edit: This did not really help. My main problem is that a dhclient background process is occupying port 68, which prevents any other process doing DHCP discovery, and I do not know any way to make the background process free it without dropping network config.

| improve this answer | |

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.