Initial commit: 0.1
[cnetworkmanager.git] / cnetworkmanager
blob07c5d4b33d25069a67df98ffb181ede8324db32e
1 #! /usr/bin/python
2 # cnetworkmanager: Command Line Interface for NetworkManager
3 # by: http://en.opensuse.org/User:Mvidner
4 # license: http://creativecommons.org/licenses/by/3.0/
6 VERSION = "0.1"
7 print "cnetworkmanager %s - Command Line Interface for NetworkManager" % VERSION
9 import sys
10 import dbus
11 import gobject
13 from dbus.mainloop.glib import DBusGMainLoop
14 DBusGMainLoop(set_as_default=True)
16 HELP = False
17 NMVER = "07"
18 DUMP_DEVS = False
19 DUMP_APS = False
20 DUMP_ACTCON = False
21 DUMP_SYSCON = False
22 DUMP_USRCON = False
24 bus = dbus.SystemBus()
26 # FOOC = connection (service) string
27 # FOOI = interface string
28 # fooo = object
29 # fooi = interface
30 # foopi = property interface
31 NMC = 'org.freedesktop.NetworkManager'
32 NMI = NMC
33 PI = 'org.freedesktop.DBus.Properties'
34 SSC = "org.freedesktop.NetworkManagerSystemSettings"
35 USC = "org.freedesktop.NetworkManagerUserSettings"
38 def introspect(obj):
39 ii = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
40 print ii.Introspect()
42 # TODO: pull them from introspection.xml
43 NM_STATE = ["UNKNOWN", "ASLEEP", "CONNECTING", "CONNECTED", "DISCONNECTED",]
45 def dump_nm(opath):
46 nmo = bus.get_object(NMC, opath)
47 nmi = dbus.Interface(nmo, NMI)
48 nmpi = dbus.Interface(nmo, PI)
50 print "state:", NM_STATE[nmpi.Get(NMI, "State")]
51 print "wifi:", nmpi.Get(NMI, "WirelessEnabled")
52 print "wifi hw:", nmpi.Get(NMI, "WirelessHardwareEnabled")
53 # nmpi.Set(NMI, "WirelessEnabled", True)
54 # nmi.Sleep(True)
56 if DUMP_DEVS:
57 devices = nmi.GetDevices()
58 for device in devices:
59 print device
60 dump_device(device)
62 if DUMP_ACTCON:
63 print "Active Connections"
64 aconns = nmpi.Get(NMI, "ActiveConnections")
65 for aconn in aconns:
66 print aconn
67 dump_aconn(aconn)
69 def dump_aconn(opath):
70 co = bus.get_object(NMC, opath)
71 copi = dbus.Interface(co, PI)
72 for P in ["ServiceName", "Connection", "SpecificObject", "SharedServiceName", "SharedConnection", "Devices"]:
73 print " %s: %s" % (P, copi.Get(NMI, P))
75 def dump_nm_06(opath):
76 nmo = bus.get_object(NMC, opath)
77 nmi = dbus.Interface(nmo, NMI)
79 print "state:", NM_STATE[nmi.state()]
81 if DUMP_DEVS:
82 devices = nmi.getDevices()
83 for device in devices:
84 print device
85 dump_device_06(device)
87 if DUMP_ACTCON:
88 print "Active Connections"
89 # FIXME
90 print nmi.GetActiveConnections()
92 def bitmask_str(map, value):
93 ret = []
94 for mask, s in map.iteritems():
95 if value & mask: ret.append(s)
96 return ",".join(ret)
98 NM_DEVICE_CAP = {1: "NM_SUPPORTED", 2: "CARRIER_DETECT",}
100 NM_DEVICE_STATE = [
101 "UNKNOWN", "DOWN", "DISCONNECTED", "PREPARE", "CONFIG",
102 "NEED_AUTH", "IP_CONFIG", "ACTIVATED", "FAILED", "CANCELLED",]
104 DEVICE_TYPE = ["UNKNOWN", "802_3_ETHERNET", "802_11_WIRELESS", "GSM", "CDMA",]
106 def dump_device(opath):
107 devo = bus.get_object(NMC, opath)
108 devpi = dbus.Interface(devo, PI)
109 # "Ip4Config", only for NM_DEVICE_STATE_ACTIVATED
110 for P in ["Udi", "Interface", "Driver", "Ip4Address"]:
111 print " %s: %s" % (P, devpi.Get(NMI, P))
112 caps = devpi.Get(NMI, "Capabilities")
113 print " Capabilities:", bitmask_str(NM_DEVICE_CAP, caps)
114 print " Dev State:", NM_DEVICE_STATE[devpi.Get(NMI, "State")]
115 dt = DEVICE_TYPE[devpi.Get(NMI, "DeviceType")]
116 print " Dev Type:", dt
117 if dt == "802_3_ETHERNET":
118 dump_device_eth(opath)
119 elif dt == "802_11_WIRELESS":
120 dump_device_wifi(opath)
122 def dump_device_06(opath):
123 devo = bus.get_object(NMC, opath)
124 introspect(devo)
125 devi = dbus.Interface(devo, NMI + ".Device")
126 print " Driver:", devi.getDriver()
127 props = devi.getProperties()
128 # TODO: annotate struct
129 for prop in props:
130 print " ", prop
132 def dump_device_eth(opath):
133 devo = bus.get_object(NMC, opath)
134 devpi = dbus.Interface(devo, PI)
135 for P in ["HwAddress", "Speed", "Carrier"]:
136 print " %s: %s" % (P, devpi.Get(NMI, P))
138 IW_MODE = ["AUTO", "ADHOC", "INFRA", "MASTER", "REPEAT", "SECOND", "MONITOR",]
140 NM_802_11_DEVICE_CAP = {1:"CIPHER_WEP40", 2:"CIPHER_WEP104", 4:"CIPHER_TKIP",
141 8:"CIPHER_CCMP", 16:"WPA", 32:"RSN",}
143 def dump_device_wifi(opath):
144 devo = bus.get_object(NMC, opath)
145 devpi = dbus.Interface(devo, PI)
146 print " Dev Mode:", IW_MODE[devpi.Get(NMI, "Mode")]
147 wcaps = devpi.Get(NMI, "WirelessCapabilities")
148 print " Wifi Capabilities:", bitmask_str(NM_802_11_DEVICE_CAP, wcaps)
149 for P in ["HwAddress", "Bitrate", "ActiveAccessPoint"]:
150 print " %s: %s" % (P, devpi.Get(NMI, P))
151 if DUMP_APS:
152 print " Access Points"
153 wdevi = dbus.Interface(devo, NMI + ".Device.Wireless")
154 aps = wdevi.GetAccessPoints()
155 for ap in aps:
156 print " ", ap
157 dump_access_point(ap)
159 NM_802_11_AP_FLAGS = {1: "PRIVACY",}
161 NM_802_11_AP_SEC = {
162 1: "PAIR_WEP40", 2: "PAIR_WEP104", 4: "PAIR_TKIP", 8: "PAIR_CCMP",
163 16: "GROUP_WEP40", 32: "GROUP_WEP104", 64: "GROUP_TKIP",
164 128: "GROUP_CCMP", 256: "KEY_MGMT_PSK", 512: "KEY_MGMT_802_1X",}
166 def ssid_str(array):
167 s = ""
168 for b in array:
169 s = s + ("%c" % b)
170 return s
172 def dump_access_point(opath):
173 apo = bus.get_object(NMC, opath)
174 appi = dbus.Interface(apo, PI)
175 print " Ssid:", ssid_str(appi.Get(NMI, "Ssid"))
176 # FIXME strength as number
177 for P in ["Frequency", "HwAddress", "MaxBitrate", "Strength"]:
178 print " %s: %s" % (P, appi.Get(NMI, P))
179 print " AP Mode:", IW_MODE[appi.Get(NMI, "Mode")]
180 print " AP Flags:", bitmask_str(NM_802_11_AP_FLAGS,
181 appi.Get(NMI, "Flags"))
182 print " AP WPA Flags:", bitmask_str(NM_802_11_AP_SEC,
183 appi.Get(NMI, "WpaFlags"))
184 print " AP RSN Flags:", bitmask_str(NM_802_11_AP_SEC,
185 appi.Get(NMI, "RsnFlags"))
187 def dump_settings(svc,obj):
188 si = dbus.Interface(obj, 'org.freedesktop.NetworkManagerSettings')
189 conns = si.ListConnections()
190 for conn in conns:
191 dump_settings_conn(svc, conn)
193 def dump_settings_dict(settings):
194 for n1, v1 in settings.iteritems():
195 print " ",n1
196 for n2, v2 in v1.iteritems():
197 print " %s: %s" % (n2, v2)
199 def dump_settings_conn(svc, opath):
200 print "Conn:",opath
201 co = bus.get_object(svc, opath)
202 ci = dbus.Interface(co, 'org.freedesktop.NetworkManagerSettings.Connection')
203 print " Id:", ci.GetID()
204 settings = ci.GetSettings()
205 dump_settings_dict(settings)
206 si = dbus.Interface(co, 'org.freedesktop.NetworkManagerSettings.Connection.Secrets')
207 print " SECRETS"
208 secrets = si.GetSecrets("802-11-wireless-security",[],False)
209 dump_settings_dict(secrets)
211 def loop():
212 loop = gobject.MainLoop()
213 try:
214 loop.run()
215 except:
216 print "Loop exited"
219 # main
221 for arg in sys.argv:
222 if arg == "-h" or arg == "--help":
223 HELP = True
224 elif arg == "--06":
225 NMVER = "06"
226 elif arg == "-d" or arg == "--dev":
227 DUMP_DEVS = True
228 elif arg == "-c" or arg == "--actcon":
229 DUMP_ACTCON = True
230 elif arg == "-u" or arg == "--usrcon":
231 DUMP_USRCON = True
232 elif arg == "-s" or arg == "--syscon":
233 DUMP_SYSCON = True
234 elif arg == "-a" or arg == "--ap":
235 DUMP_APS = True
236 DUMP_DEVS = True # aps are under wifi devs
237 elif arg == "--loop":
238 LOOP = True
240 if HELP:
241 print "Options:"
242 print " -6, --06 assume NetworkManager 0.6 API"
243 print " -d, --dev list devices"
244 print " -c, --actcon list active connections"
245 print " -s, --syscon list system connection settings"
246 print " -u, --usrcon list user connection settings (can CRASH nm-applet)"
247 print " -a, --ap list found access points"
248 sys.exit(0)
250 # TODO autodetect (try getDevices?)
251 if NMVER == "06":
252 dump_nm_06('/org/freedesktop/NetworkManager')
253 else:
254 dump_nm('/org/freedesktop/NetworkManager')
256 if DUMP_SYSCON:
257 print "SYSTEM Connections"
258 sso = bus.get_object(SSC, '/org/freedesktop/NetworkManagerSettings')
259 dump_settings(SSC, sso)
261 if DUMP_USRCON:
262 print "USER Connections"
263 uso = bus.get_object(USC, '/org/freedesktop/NetworkManagerSettings')
264 dump_settings(USC, uso)