6

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
  • I'm not sure you can... are you trying to check internal or external IPs? If you are looking at internal IPs, you can generally assume they're dynamic. If you are looking at external IPs, sometimes asking your ISP will get you an answer - subsequently, if you're on a residential ISP, assume its dynamic, as only small-business-class ISP plans would be static IPs.
    – Thomas Ward
    Jun 9, 2011 at 19:38

5 Answers 5

3

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
1

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.

0

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
  • This do not work with NetworkManager, the default in Ubuntu.
    – enzotib
    Aug 6, 2011 at 18:37
  • What do you mean by 'not working with NetworkManager'? It's output is not updated according to the running config? The output is surely working ok...
    – Pavlos G.
    Aug 6, 2011 at 21:31
  • When using NetworkManager, /etc/network/interfaces do not contain any information on eth0 or wlan0, or similar
    – enzotib
    Aug 6, 2011 at 21:33
0

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.

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .