How to set Ipv4 addresses with dbus-python (Hotspot and ethernet)

665 views Asked by At

(fairly new to networking)

I'm trying to setup a small, yet somewhat complicated network settings on my ubuntu 18.04 machine.

The topology of the network: Ubuntu machine (called "the server") will act as the DHCP server for both hotspot and ethernet. connected to the ubuntu machine are 2 ubuntu machine clients and a camera.

I've implemented "the server" with python-dbus library, to set up/down a hotspot connection, which works as intended. but my problem is how to manage the ip addresses and the routing.

i'll elaborate on 2 problems i am facing:

  1. in order to change the ipv4 address for the Hotspot AP, i found out i could edit a file: "/etc/NetworkManager/system-connections/", adding another line: "address1=X.Y.Z.W" (my desired ip address).

but editing the file isn't the proper way for my requirements, i would rather do it from the code itself. which changes do i need to make to the code in order to make the same changes?

this is how the code connection object of dbus looks like:

def get_hotspot_struct(iface, uuid, ssid, password):
    s_con = dbus.Dictionary({
        'type': '802-11-wireless',
        'uuid': uuid,
        'id': 'PixellotHotspot',
        'interface-name': iface,
    })

    s_wifi = dbus.Dictionary({
        'ssid': dbus.ByteArray(ssid.encode()),
        'mode': 'ap',
        'band': 'bg',
        'channel': dbus.UInt32(1),
    })

    s_wsec = dbus.Dictionary({
        'key-mgmt': 'wpa-psk',
        'psk': password,
    })

    s_ipv4 = dbus.Dictionary({
        'method': 'shared',
    })

    s_ipv6 = dbus.Dictionary({
        'method': 'ignore',
    })

    con = dbus.Dictionary({
        'connection': s_con,
        '802-11-wireless': s_wifi,
        '802-11-wireless-security': s_wsec,
        'ipv4': s_ipv4,
        'ipv6': s_ipv6,
    })

    logger.info('Getting hotspot connection template')
    logger.info(con)

    return con
  1. Can i do the same for ethernet wired connections? so far what ive figured is that I can edit "/etc/netplan/01-netconf.yaml" in order to set dhcp to false, and se an ip "X.Y.Z.W" (desired) for ethernet interface eth0.

but that seem to only work on the server, when i connect the ubuntu clients with ethernet wire to the server, the server wont give the clients any ip at all. It does for the hotspot, but not for the ethernet.

I know my problem is very specific and all-over-the-place, but i would appreciate any help. Post here/sendme email/ Facebook me(Yves Halimi) if you have knowledge about this issue. Will compensate help!!

1

There are 1 answers

0
thaller On

The D-Bus API is documented in man nm-settings-dbus.

To NetworkManager, it's always about creating connection profiles and activating them. So if you have code that can create one profile, another profile works basically the same -- just some keys will be different.

I find it helpful to use one of the other NetworkManager clients, and compare with what they do. For example, you could also just create the profile with nmcli connection add type ..., then get the D-Bus path via nmcli -f all connection show and finally, look at how the profiles looks on D-Bus:

busctl -j call org.freedesktop.NetworkManager /org/freedesktop/NetworkManager/Settings/1 org.freedesktop.NetworkManager.Settings.Connection GetSettings 

See examples upstream: python+dbus

Maybe you'll find it easier to use python + pygobject + libnm. In that case, see examples here. The main downside is that you'll have an additional dependency (pygobject). libnm isn't an additional dependency, you'll already have that if you use NetworkManager.