2 # cnetworkmanager: Command Line Interface for NetworkManager
3 # by: http://en.opensuse.org/User:Mvidner
4 # license: http://creativecommons.org/licenses/by/3.0/
7 print "cnetworkmanager %s - Command Line Interface for NetworkManager" % VERSION
15 from dbus
.mainloop
.glib
import DBusGMainLoop
16 DBusGMainLoop(set_as_default
=True)
26 bus
= dbus
.SystemBus()
28 # FOOC = connection (service) string
29 # FOOI = interface string
32 # foopi = property interface
33 NMC
= 'org.freedesktop.NetworkManager'
35 PI
= 'org.freedesktop.DBus.Properties'
36 SSC
= "org.freedesktop.NetworkManagerSystemSettings"
37 USC
= "org.freedesktop.NetworkManagerUserSettings"
41 ii
= dbus
.Interface(obj
, 'org.freedesktop.DBus.Introspectable')
44 # TODO: pull them from introspection.xml
45 NM_STATE
= ["UNKNOWN", "ASLEEP", "CONNECTING", "CONNECTED", "DISCONNECTED",]
48 nmo
= bus
.get_object(NMC
, opath
)
49 nmi
= dbus
.Interface(nmo
, NMI
)
50 nmpi
= dbus
.Interface(nmo
, PI
)
52 print "state:", NM_STATE
[nmpi
.Get(NMI
, "State")]
53 print "wifi:", nmpi
.Get(NMI
, "WirelessEnabled")
54 print "wifi hw:", nmpi
.Get(NMI
, "WirelessHardwareEnabled")
55 # nmpi.Set(NMI, "WirelessEnabled", True)
59 devices
= nmi
.GetDevices()
60 for device
in devices
:
65 print "Active Connections"
66 aconns
= nmpi
.Get(NMI
, "ActiveConnections")
71 def dump_aconn(opath
):
72 co
= bus
.get_object(NMC
, opath
)
73 copi
= dbus
.Interface(co
, PI
)
74 for P
in ["ServiceName", "Connection", "SpecificObject", "SharedServiceName", "SharedConnection", "Devices"]:
75 print " %s: %s" % (P
, copi
.Get(NMI
, P
))
77 def dump_nm_06(opath
):
78 nmo
= bus
.get_object(NMC
, opath
)
79 nmi
= dbus
.Interface(nmo
, NMI
)
81 print "state:", NM_STATE
[nmi
.state()]
84 devices
= nmi
.getDevices()
85 for device
in devices
:
87 dump_device_06(device
)
90 print "Active Connections"
92 print nmi
.GetActiveConnections()
94 def bitmask_str(map, value
):
96 for mask
, s
in map.iteritems():
97 if value
& mask
: ret
.append(s
)
102 ret
.append("%d" % (i32
% 256))
104 ret
.append("%d" % (i32
% 256))
106 ret
.append("%d" % (i32
% 256))
108 ret
.append("%d" % (i32
% 256))
112 NM_DEVICE_CAP
= {1: "NM_SUPPORTED", 2: "CARRIER_DETECT",}
115 "UNKNOWN", "UNMANAGED", "UNAVAILABLE", "DISCONNECTED", "PREPARE", "CONFIG",
116 "NEED_AUTH", "IP_CONFIG", "ACTIVATED", "FAILED",]
118 DEVICE_TYPE
= ["UNKNOWN", "802_3_ETHERNET", "802_11_WIRELESS", "GSM", "CDMA",]
120 def dump_ip4config(opath
):
122 o
= bus
.get_object(NMC
, opath
)
123 pi
= dbus
.Interface(o
, PI
)
124 for P
in ["Address", "Netmask", "Broadcast", "Gateway",]:
125 print " %s: %s" % (P
, ip_str(pi
.Get(NMI
, P
)))
126 for P
in ["Hostname", "Nameservers", "Domains", "NisDomain", "NisServers"]:
127 print " %s: %s" % (P
, pi
.Get(NMI
, P
))
130 def dump_device(opath
):
131 devo
= bus
.get_object(NMC
, opath
)
132 devpi
= dbus
.Interface(devo
, PI
)
133 # "Ip4Config", only for NM_DEVICE_STATE_ACTIVATED
134 for P
in ["Udi", "Interface", "Driver",]:
135 print " %s: %s" % (P
, devpi
.Get(NMI
, P
))
136 addr
= devpi
.Get(NMI
, "Ip4Address")
137 print " Ip4Address:", ip_str(addr
)
138 caps
= devpi
.Get(NMI
, "Capabilities")
139 print " Capabilities:", bitmask_str(NM_DEVICE_CAP
, caps
)
140 state
= NM_DEVICE_STATE
[devpi
.Get(NMI
, "State")]
141 print " Dev State:", state
142 if state
== "ACTIVATED":
143 dump_ip4config(devpi
.Get(NMI
, "Ip4Config"))
144 dt
= DEVICE_TYPE
[devpi
.Get(NMI
, "DeviceType")]
145 print " Dev Type:", dt
146 if dt
== "802_3_ETHERNET":
147 dump_device_eth(opath
)
148 elif dt
== "802_11_WIRELESS":
149 dump_device_wifi(opath
)
151 def dump_device_06(opath
):
152 devo
= bus
.get_object(NMC
, opath
)
154 devi
= dbus
.Interface(devo
, NMI
+ ".Device")
155 print " Driver:", devi
.getDriver()
156 props
= devi
.getProperties()
157 # TODO: annotate struct
161 def dump_device_eth(opath
):
162 devo
= bus
.get_object(NMC
, opath
)
163 devpi
= dbus
.Interface(devo
, PI
)
164 for P
in ["HwAddress", "Speed", "Carrier"]:
165 print " %s: %s" % (P
, devpi
.Get(NMI
, P
))
167 IW_MODE
= ["AUTO", "ADHOC", "INFRA", "MASTER", "REPEAT", "SECOND", "MONITOR",]
169 NM_802_11_DEVICE_CAP
= {1:"CIPHER_WEP40", 2:"CIPHER_WEP104", 4:"CIPHER_TKIP",
170 8:"CIPHER_CCMP", 16:"WPA", 32:"RSN",}
172 def dump_device_wifi(opath
):
173 devo
= bus
.get_object(NMC
, opath
)
174 devpi
= dbus
.Interface(devo
, PI
)
175 print " Dev Mode:", IW_MODE
[devpi
.Get(NMI
, "Mode")]
176 wcaps
= devpi
.Get(NMI
, "WirelessCapabilities")
177 print " Wifi Capabilities:", bitmask_str(NM_802_11_DEVICE_CAP
, wcaps
)
178 for P
in ["HwAddress", "Bitrate", "ActiveAccessPoint"]:
179 print " %s: %s" % (P
, devpi
.Get(NMI
, P
))
181 print " Access Points"
182 wdevi
= dbus
.Interface(devo
, NMI
+ ".Device.Wireless")
183 aps
= wdevi
.GetAccessPoints()
186 dump_access_point(ap
)
188 NM_802_11_AP_FLAGS
= {1: "PRIVACY",}
191 1: "PAIR_WEP40", 2: "PAIR_WEP104", 4: "PAIR_TKIP", 8: "PAIR_CCMP",
192 16: "GROUP_WEP40", 32: "GROUP_WEP104", 64: "GROUP_TKIP",
193 128: "GROUP_CCMP", 256: "KEY_MGMT_PSK", 512: "KEY_MGMT_802_1X",}
201 def dump_access_point(opath
):
202 apo
= bus
.get_object(NMC
, opath
)
203 appi
= dbus
.Interface(apo
, PI
)
204 print " Ssid:", ssid_str(appi
.Get(NMI
, "Ssid"))
205 # FIXME strength as number
206 for P
in ["Frequency", "HwAddress", "MaxBitrate",]:
207 print " %s: %s" % (P
, appi
.Get(NMI
, P
))
208 print " Strength:", int(appi
.Get(NMI
, "Strength"))
209 print " AP Mode:", IW_MODE
[appi
.Get(NMI
, "Mode")]
210 print " AP Flags:", bitmask_str(NM_802_11_AP_FLAGS
,
211 appi
.Get(NMI
, "Flags"))
212 print " AP WPA Flags:", bitmask_str(NM_802_11_AP_SEC
,
213 appi
.Get(NMI
, "WpaFlags"))
214 print " AP RSN Flags:", bitmask_str(NM_802_11_AP_SEC
,
215 appi
.Get(NMI
, "RsnFlags"))
217 def dump_settings(svc
, obj
):
218 si
= dbus
.Interface(obj
, 'org.freedesktop.NetworkManagerSettings')
219 conns
= si
.ListConnections()
221 dump_settings_conn(svc
, conn
)
223 def dump_system_settings(svc
, obj
):
224 sspi
= dbus
.Interface(obj
, PI
)
225 print "Unmanaged Devices"
226 umds
= sspi
.Get(NMI
, "UnmanagedDevices")
229 dump_settings_conn(svc
, conn
)
231 def dump_settings_dict(settings
):
232 for n1
, v1
in settings
.iteritems():
234 for n2
, v2
in v1
.iteritems():
235 print " %s: %s" % (n2
, v2
)
237 def dump_settings_conn(svc
, opath
):
239 co
= bus
.get_object(svc
, opath
)
240 ci
= dbus
.Interface(co
, 'org.freedesktop.NetworkManagerSettings.Connection')
241 print " Id:", ci
.GetID()
242 settings
= ci
.GetSettings()
243 dump_settings_dict(settings
)
244 si
= dbus
.Interface(co
, 'org.freedesktop.NetworkManagerSettings.Connection.Secrets')
246 secrets
= si
.GetSecrets("802-11-wireless-security",[],False)
247 dump_settings_dict(secrets
)
250 loop
= gobject
.MainLoop()
260 if arg
== "-h" or arg
== "--help":
264 elif arg
== "-d" or arg
== "--dev":
266 elif arg
== "-c" or arg
== "--actcon":
268 elif arg
== "-u" or arg
== "--usrcon":
270 elif arg
== "-s" or arg
== "--syscon":
272 elif arg
== "-a" or arg
== "--ap":
274 DUMP_DEVS
= True # aps are under wifi devs
275 elif arg
== "--loop":
280 print " -6, --06 assume NetworkManager 0.6 API"
281 print " -d, --dev list devices"
282 print " -c, --actcon list active connections"
283 print " -s, --syscon list system connection settings"
284 print " -u, --usrcon list user connection settings (can CRASH nm-applet)"
285 print " -a, --ap list found access points"
288 nmp
= '/org/freedesktop/NetworkManager'
289 # TODO autodetect (try getDevices?)
296 print "SYSTEM Connections"
297 sso
= bus
.get_object(SSC
, '/org/freedesktop/NetworkManagerSettings')
298 dump_settings(SSC
, sso
)
299 dump_system_settings(SSC
, sso
)
302 print "USER Connections"
303 uso
= bus
.get_object(USC
, '/org/freedesktop/NetworkManagerSettings')
304 dump_settings(USC
, uso
)
308 class UserSettings(dbus
.service
.Object
):
309 def __init__(self
, opath
):
310 dbus
.service
.Object
.__init
__(self
, bus
, opath
)
312 @dbus.service
.method(dbus_interface
='org.freedesktop.NetworkManagerSettings',
313 in_signature
='', out_signature
='ao')
314 def ListConnections(self
):
315 return ["/org/freedesktop/NetworkManagerSettings/1"]
317 @dbus.service
.signal(dbus_interface
='org.freedesktop.NetworkManagerSettings',
319 def NewConnection(self
, opath
):
320 print "signalling newconn:", opath
322 class Connection(dbus
.service
.Object
):
323 def __init__(self
, opath
):
324 dbus
.service
.Object
.__init
__(self
, bus
, opath
)
326 @dbus.service
.method(dbus_interface
='org.freedesktop.NetworkManagerSettings.Connection',
327 in_signature
='', out_signature
='a{sa{sv}}')
328 def GetSettings(self
):
332 'type': '802-11-wireless',
335 'ssid': dbus
.ByteArray('frogs'),
336 'mode': 'infrastructure',
337 'security': '802-11-wireless-security',
339 '802-11-wireless-security': {
342 # 'auth-alg': 'shared',
345 @dbus.service
.method(dbus_interface
='org.freedesktop.NetworkManagerSettings.Connection.Secrets',
346 in_signature
='sasb', out_signature
='a{sa{sv}}')
347 def GetSecrets(self
,tag
,hints
,ask
):
349 '802-11-wireless-security': {
353 'wep-key0': '66666666666666666666666666', # secret hint
358 nmo
= bus
.get_object(NMC
, nmp
)
359 nmi
= dbus
.Interface(nmo
, NMI
)
361 us
= UserSettings("/org/freedesktop/NetworkManagerSettings")
362 c
= Connection("/org/freedesktop/NetworkManagerSettings/1")
364 bus
.request_name(USC
)
365 #os.system("sleep 3")
373 nmi
.ActivateConnection(USC
,
374 "/org/freedesktop/NetworkManagerSettings/1",
375 "/org/freedesktop/Hal/devices/net_00_11_d8_70_00_2f",
376 "/org/freedesktop/NetworkManager/AccessPoint/0")
380 def my_sig_handler(*args
, **kwargs
):
381 print "got signal:", args
, kwargs
385 bus
.add_signal_receiver(my_sig_handler
)
387 us
.NewConnection("/org/freedesktop/NetworkManagerSettings/1")