Use NetworkManager via D-BUS to get (and later set) DHCP status on eth0

I’m looking for a way to use D-BUS and access the Network Manager to determine if eth0 is using DHCP or not. Later I want to set dhcp/static (and ip, etc), but first task is to just determine if eth0 is set up for static IP or dhcp. I can read the other settings like the gateway, address, netmask, but I can’t find a setting for DHCP. Anyone have an example on how to get the dhcp status property? The documentation seems to indicate there should be a method property under the Ipv4Config, but it doesn’t seem to exist.

Hi,

I know it’s been a while, and you’ve probably found a better solution (by using nmcli or something).
But I did want to respond with what I’ve found for whoever might be interested.

Convenience var:

DEFAULT_SEND="dbus-send --system --print-reply --reply-timeout=1000"
DEFAULT_METHOD="${DEFAULT_SEND} --type=method_call"

First to find the correct device:

root@balena4:/usr/src/app# $DEFAULT_METHOD --dest='org.freedesktop.NetworkManager' '/org/freedesktop/NetworkManager' 'org.freedesktop.NetworkManager.GetDeviceByIpIface' string:'eth0'
method return time=1676401690.826238 sender=:1.5 -> destination=:1.110 serial=8780 reply_serial=2
   object path "/org/freedesktop/NetworkManager/Devices/2"

And then to find the connection settings:

root@balena4:/usr/src/app# $DEFAULT_METHOD --dest='org.freedesktop.NetworkManager' '/org/freedesktop/NetworkManager/Devices/2' 'org.freedesktop.NetworkManager.Device.GetAppliedConnection' uint32:0
method return time=1676402023.429151 sender=:1.5 -> destination=:1.115 serial=9240 reply_serial=2
   array [
      dict entry(
         string "connection"
         array [
            dict entry(
               string "autoconnect-priority"
               variant                   int32 -999
            )
            dict entry(
               string "id"
               variant                   string "Wired connection 1"
            )
            dict entry(
               string "interface-name"
               variant                   string "eth0"
            )
            dict entry(
               string "permissions"
               variant                   array [
                  ]
            )
            dict entry(
               string "timestamp"
               variant                   uint64 1675796408
            )
            dict entry(
               string "type"
               variant                   string "802-3-ethernet"
            )
            dict entry(
               string "uuid"
               variant                   string "8ffa7d04-db48-3271-8fbc-4cca019f2190"
            )
         ]
      )
      dict entry(
         string "802-3-ethernet"
         array [
            dict entry(
               string "auto-negotiate"
               variant                   boolean false
            )
            dict entry(
               string "mac-address-blacklist"
               variant                   array [
                  ]
            )
            dict entry(
               string "s390-options"
               variant                   array [
                  ]
            )
         ]
      )
      dict entry(
         string "ipv4"
         array [
            dict entry(
               string "address-data"
               variant                   array [
                  ]
            )
            dict entry(
               string "addresses"
               variant                   array [
                  ]
            )
            dict entry(
               string "dns-search"
               variant                   array [
                  ]
            )
            dict entry(
               string "method"
               variant                   string "auto"
            )
            dict entry(
               string "route-data"
               variant                   array [
                  ]
            )
            dict entry(
               string "routes"
               variant                   array [
                  ]
            )
         ]
      )
      dict entry(
         string "ipv6"
         array [
            dict entry(
               string "address-data"
               variant                   array [
                  ]
            )
            dict entry(
               string "addresses"
               variant                   array [
                  ]
            )
            dict entry(
               string "dns-search"
               variant                   array [
                  ]
            )
            dict entry(
               string "method"
               variant                   string "auto"
            )
            dict entry(
               string "route-data"
               variant                   array [
                  ]
            )
            dict entry(
               string "routes"
               variant                   array [
                  ]
            )
         ]
      )
      dict entry(
         string "proxy"
         array [
         ]
      )
   ]
   uint64 1

As you can see, this returns the used connection settings.
The entry you’re interested in here is ipv4.method.

The Settings object has a method that you can use to change these fields.

1 Like

Thanks for these details! No, I haven’t solved it yet, I will try this approach and report back, thanks!

Took a while before I got to test this out. It worked great, so the key was to get the “applied connection” and the ipv4/method property was present there.

1 Like

Glad to hear it worked out for you :slight_smile: