Open mahiuchun opened 12 years ago
When I wrote RefreshIP.py for Ubuntu 9.10, I found it really hard to control NM using its DBus interface. There is a command line tool cnetworkmananger which is helpful to control NM. We only need to refresh the dynamic ip address from command line, but it seems to be not supported from command line.
As I stated in the origin post. I don't think using NM for a CLI tool is philosophically appropriate. I know that NM is shipped with some Desktop-oriented Linux distributions. However, NM has bad reputation among some experienced Linux users.
To refresh IP address by DHCP, you can simply use dhclient(8) . http://linux.vbird.org/linux_server/0140networkcommand.php#dhclient http://manpages.ubuntu.com/manpages/precise/man8/dhclient.8.html
BTW, NM has an official command line interface since version 0.8.1, and it is called nmcli(1) . http://live.gnome.org/NetworkManager/ReleaseProcess http://manpages.ubuntu.com/manpages/precise/man1/nmcli.1.html
FYI, YaH3C uses dhcpd(8) , whose usage is similar to dhclient(8) . https://github.com/humiaozuzu/YaH3C/blob/master/yah3c/plugins/auto_dhcp.py http://manpages.ubuntu.com/manpages/precise/man8/dhcpd.8.html
The python script to refresh IP address works as follows
#!/usr/bin/python
import dbus
nm = dbus.SystemBus().get_object('org.freedesktop.NetworkManager','/org/freedesktop/NetworkManager')
nmService = ...
nmConnection = ...
nmDevice = ...
nmSpecificObject = ...
dbus.Interface(nm,'org.freedesktop.NetworkManager').ActivateConnection(nmService, nmConnection, nmDevice, nmSpecificObject)
We can also use the command line tool "dbus-send" to talk to NM as the following shell script, which is similar to using the python script.
#!/bin/sh
echo Refreshing IP address...
#nmService=string:"org.freedesktop.NetworkManagerSystemSettings"
#nmConnection='objpath:/org/freedesktop/NetworkManagerSettings/0'
#nmDevice='objpath:/org/freedesktop/Hal/devices/net_00_16_36_52_53_12'
#nmSpecificObject='objpath:/'
dbus-send --system --type=method_call \
--dest=org.freedesktop.NetworkManager \
/org/freedesktop/NetworkManager \
org.freedesktop.NetworkManager.ActivateConnection \
string:"org.freedesktop.NetworkManagerSystemSettings" \
objpath:/org/freedesktop/NetworkManagerSettings/0 \
objpath:/org/freedesktop/Hal/devices/net_00_16_36_52_53_12 \
objpath:/
Thank you for your work.
But my concern is not about how to talk to NM. Instead, I doubt whether we can safely assume that NM is running or even exists in users' machine. Given the very simple usage of dhcpd(8) or dhclient(8), why not use them?
I've seen ARM-Linux based embedded developing tool-kits which supports D-Bus. I guess there might be some embedded linux systems, such as an Ubuntu net-book with an ARM CPU, which should probably support the FreeDesktop.org/NetworkManager.
Third-party developers on njit-client can put an customized RefreshIP script as follows, replacing my python script:
#!/bin/sh
udhcpc --renew
killall dhclient
dhclient
Put it in another way. What's advantage of using NM? If there are multiple active ethernet interfaces, what would your current script do? Why don't you simply use some commands that can specify interface easily?
https://github.com/liuqun/njit8021xclient/blob/master/src/RefreshIP.py
In summary, this Python script uses D-Bus to ask NM(NetworkManager) to refresh IP.
1.Python's portability is questionable. http://sheddingbikes.com/posts/1285063820.html
2.The interface selection mechanism is not consistent with njit-client. njit-client is not dependent on NM, it uses low-level APIs directly.
3.Dependency of D-Bus and NM is annoying for CLI motivated users. cfy reported that the latest version doesn't work well for him. In fact, he doesn't use NM at all.
My suggestion: Use a Shell script instead, which accepts device name as an argument. Use POSIX Shell to archive maximal portability.
NM integration is elegant in some aspect. But I guess call NM in a GUI front-end is better. Information from NM can help casual user pick a right interface.