Check if IP is static or dynamic programmatically

I'm trying to determine whether my IP has been statically or dynamically assigned in Ubuntu 11.04 from within C. The closest I've come is examining /var/lib/dhcp/dhclient.leases, but that doesn't change if a static IP is assigned (as it caches DHCP leases for future connection attempts).

So... is there a command line utility (for Ubuntu) that will allow me to check if DHCP is enabled or disabled?

Alternatively: the machine is a server dedicated to one application; would it be easier just to maintain an environment variable that keeps track of DHCP status?

1

5 Answers

You can put scripts in /etc/network/if-up.d to be executed when an interface is activated (either by ifup or by Network Manager). These scripts receive information about the interface, including the environment variable $METHOD set to static or dhcp. This is documented in the interfaces(5) man page.

#!/bin/sh
## This is /etc/network/if-up.d/zzzz_alex_notify_myserver
case $LOGICAL in eth0) case $METHOD in dhcp) <notify server that it's on DHCP>;; static) <notify server that it's on a static IP configuration>;; esac;;
esac

Another way is to use NetworkManager tools, namely nm-tool and nmcli.

I writed the following script on this:

#!/bin/sh
# get the connection id of the active connection
get_con_id() { nm-tool | awk ' $1 == "-" { dev = $3 id = dev if (NF > 4 && match($0, "\\[(.*)\\]", a)) id = a[1] } /^ / && $1 == "State:" && $2 == "connected" { print id }'
}
# get the address type of the active connection
nmcli con list id "$(get_con_id)" | awk ' $1 == "ipv4.method:" { if ($2 == "manual") print "static" else if ($2 == "auto") print "dynamic" else print "unknown" }'

It probably break in non usual cases, e.g. where you have more than one active connection, but could be a starting point on which to elaborate.

Also, if you modify a connection, the script will see the new settings, even if the connection has not been restarted, so they are not in use yet.

I've written this command to get the DHCP configured interfaces in a shell script:

ps -A -o cmd | grep -E '(/| )dhclient .'

Then you can discard interfaces not managed by dhclient.

At the moment I don't know if it works for all GNU/Linux distributions.

Its much easier to determine that:

Just disconnect the machine from network. If you can see the ip address in the ifconfig output for that interface then it is statically assigned otherwise dynamically.

regards delaflota

I guess you're referring to the IP assignment of your ubuntu machine locally and not your external/public IP.

Perhaps the following output could be of some help:

cat /etc/network/interfaces |grep ^iface\ eth0 | awk -F ' ' '{print $4}'

This will print either the word static or dhcp, depending on the current configuration of interface eth0.

You can also make small adjustments, like:

cat /etc/network/interfaces |grep ^iface\ | awk -F ' ' '{print $2, $4}'

in order to list all of the interfaces and their configuration.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like