Tabularized --device-info, --ap-info.
[cnetworkmanager.git] / cnetworkmanager
blob38f635f208d335516f7b8f0a1f5b6447960fb67e
1 #!/usr/bin/python
2 VERSION = "0.20"
4 import sys
5 import time
6 import dbus
7 from networkmanager import NetworkManager
8 from networkmanager.monitor import Monitor
9 from networkmanager.applet import NetworkManagerSettings, SYSTEM_SERVICE, USER_SERVICE
10 from networkmanager.applet.service import NetworkManagerUserSettings
11 import networkmanager.applet.settings as settings
12 from networkmanager.util import Table
14 # must be set before we ask for signals
15 from dbus.mainloop.glib import DBusGMainLoop
16 DBusGMainLoop(set_as_default=True)
17 # for calling quit
18 import gobject
19 loop = gobject.MainLoop()
20 LOOP = False
22 from optparse import OptionParser
24 op = OptionParser(version="%prog " + VERSION)
26 op.add_option("--terse",
27 action="store_true", default=False,
28 help="No table headings and padding, suitable for parsing")
30 # TODO http://docs.python.org/lib/optparse-adding-new-types.html
31 op.add_option("-w", "--wifi",
32 choices=["0","1","off","on","no","yes","false","true"],
33 metavar="BOOL",
34 help="Enable or disable wireless")
35 op.add_option("-o", "--online",
36 choices=["0","1","off","on","no","yes","false","true"],
37 metavar="BOOL",
38 help="Enable or disable network at all")
39 op.add_option("--state",
40 action="store_true", default=False,
41 help="Print the NM state")
42 op.add_option("--whe", "--wireless-hardware-enabled",
43 action="store_true", default=False,
44 help="Print whether the WiFi is enabled")
46 op.add_option("-d", "--device-list", "--dev",
47 action="store_true", default=False,
48 help="List devices")
49 op.add_option("--device-info", "--di",
50 help="Info about device DEV (by interface or UDI(TODO))",
51 metavar="DEV")
53 op.add_option("-a", "--ap-list", "--ap", "-n", "--nets",# -n is a stopgap
54 action="store_true", default=False,
55 help="List access points")
56 op.add_option("--ap-info", "--ai",
57 help="Info about access point AP (by hw address or UDI(TODO))",
58 metavar="AP")
60 op.add_option("-u", "--usrcon",
61 action="store_true", default=False,
62 help="List user connection settings")
63 op.add_option("-s", "--syscon",
64 action="store_true", default=False,
65 help="List system connection settings")
67 op.add_option("-c", "--actcon",
68 action="store_true", default=False,
69 help="List active connections")
71 op.add_option("--demo",
72 action="store_true", default=False,
73 help="Run a random demonstration of the API")
74 op.add_option("--activate-connection",
75 help="activate the KIND(user/system) connection ID on device DEV using APMAC.",
76 metavar="[KIND],ID,[DEV],[APMAC]")
77 op.add_option("-m", "--monitor",
78 action="store_true", default=False,
79 help="loop to show dbus signals")
81 op.add_option("-C", "--connect",
82 help="Connect to a wireless network SSID (creating the configuration using the key options below)",
83 metavar="SSID")
84 op.add_option("--unprotected",
85 action="store_true", default=False,
86 help="network does not require a key")
87 op.add_option("--wep-hex",
88 metavar="KEY",
89 help="use this WEP key of 26 hex digits")
90 op.add_option("--wep-pass",
91 metavar="KEY",
92 help="use this WEP passphrase")
93 op.add_option("--wpa-psk-hex",
94 metavar="KEY",
95 help="use this WPA key of 64 hex digits")
96 op.add_option("--wpa-pass",
97 metavar="KEY",
98 help="use this WPA passphrase")
100 (options, args) = op.parse_args()
102 Table.terse = options.terse
104 nm = NetworkManager()
106 true_choices = ["1", "on", "yes", "true"]
107 if options.wifi != None:
108 nm["WirelessEnabled"] = options.wifi in true_choices
109 if options.online != None:
110 nm.Sleep(not options.online in true_choices)
111 if options.state:
112 print nm["State"]
113 if options.whe:
114 print nm["WirelessHardwareEnabled"]
115 # style option: pretend that properties are methods (er, python properties)
116 # nm["WirelessEnabled"] -> nm.WirelessEnabled() (er, nm.WirelessEnabled )
118 if options.device_list:
119 devs = nm.GetDevices()
120 t = Table("Interface", "Type", "State")
121 for dev in devs:
122 t.row(dev["Interface"], dev["DeviceType"], dev["State"])
123 print t
125 # --device-info, TODO clean up
126 def get_device(dev_spec, hint):
127 candidates = []
128 # print "Hint:", hint
129 devs = NetworkManager().GetDevices()
130 for dev in devs:
131 # print dev
132 if dev._settings_type() == hint:
133 candidates.append(dev)
134 # print "Candidates:", candidates
135 if len(candidates) == 1:
136 return candidates[0]
137 for dev in devs:
138 if dev["Interface"] == dev_spec:
139 return dev
140 print "Device '%s' not found" % dev_spec
141 return None
143 def prop_table(obj, *prop_names):
144 t = Table("Property", "Value")
145 for p in prop_names:
146 t.row(p, obj[p])
147 return t
149 if options.device_info != None:
150 d = get_device(options.device_info, "no hint")
151 if d == None:
152 print "not found"
153 else:
154 props = ["Udi", "Interface", "Driver", "Capabilities",
155 # "Ip4Address", # bogus, remembers last addr even after disused
156 "State",
157 # "Ip4Config", "Dhcp4Config", # only __repr__
158 "Managed", "DeviceType"]
159 if d._settings_type() == "802-11-wireless":
160 props.extend(["Mode", "WirelessCapabilities"])
161 elif d._settings_type() == "802-3-ethernet":
162 props.extend(["Carrier"])
164 print prop_table(d, *props)
166 if options.ap_list:
167 devs = nm.GetDevices()
168 # nm.get_wifi_devices()
169 # nm.get_devices_by_type(Device.Type.WIRELESS)
170 for dev in filter(lambda d: d._settings_type() == "802-11-wireless", devs):
171 aap = dev["ActiveAccessPoint"]
172 t = Table("Active", "HwAddress", "Ssid")
173 for ap in dev.GetAccessPoints():
174 active = "*" if ap.object_path == aap.object_path else ""
175 t.row(active, ap["HwAddress"], ap["Ssid"])
176 print t
178 if options.ap_info != None:
179 devs = nm.GetDevices()
180 for dev in filter(lambda d: d._settings_type() == "802-11-wireless", devs):
181 aap = dev["ActiveAccessPoint"]
182 for ap in dev.GetAccessPoints():
183 if ap["HwAddress"] == options.ap_info:
184 t = prop_table(ap, "Flags", "WpaFlags", "RsnFlags",
185 "Ssid", "Frequency", "HwAddress",
186 "Mode", "MaxBitrate", "Strength")
187 t.row("Active", ap.object_path == aap.object_path)
188 print t
190 #def is_opath(x):
191 # return is_instance(x, str) and x[0] == "/"
193 # move this to networkmanagersettings
194 def get_connection(svc, conn_spec):
195 # if is_opath(conn_spec):
196 # return conn_spec
197 applet = NetworkManagerSettings(svc)
198 for conn in applet.ListConnections():
199 cs = conn.GetSettings()
200 if cs["connection"]["id"] == conn_spec:
201 return conn
202 print "Connection '%s' not found" % conn_spec
203 return None
205 def get_connection_devtype(conn):
206 cs = conn.GetSettings()
207 return cs["connection"]["type"]
209 def list_conections(svc):
210 acs = nm["ActiveConnections"]
211 acos = map(lambda a: a["Connection"].object_path, acs)
213 applet = NetworkManagerSettings(svc)
214 for conn in applet.ListConnections():
215 cs = conn.GetSettings()
216 active = "*" if conn.object_path in acos else " "
217 print active, cs["connection"]["id"], cs["connection"]["type"]
219 if options.usrcon:
220 list_conections(USER_SERVICE)
221 if options.syscon:
222 list_conections(SYSTEM_SERVICE)
224 # this shows we do need to add __str__ to the objects
225 if options.actcon:
226 acs = nm["ActiveConnections"]
227 for ac in acs:
228 cid = ac["Connection"].GetSettings()["connection"]["id"]
229 try:
230 apmac = ac["SpecificObject"]["HwAddress"]
231 except: # no AP for wired. TODO figure out "/" object
232 apmac = ""
233 devs = ", ".join(map(lambda d: d["Interface"], ac["Devices"]))
234 hdr = "(has default route)" if ac["Default"] else ""
235 print ac["State"], cid, apmac, devs, hdr
237 if options.monitor:
238 m = Monitor()
239 LOOP = True
241 def print_state_changed(*args):
242 print time.strftime("(%X)"),
243 print "State:", ", ".join(map(str,args))
245 if options.connect != None:
246 ssid = options.connect
247 try:
248 us = NetworkManagerUserSettings([]) # request_name may fail
249 except dbus.exceptions.NameExistsException, e:
250 print "Another applet is running:", e
251 sys.exit(1)
253 c = None
254 if options.unprotected:
255 c = settings.WiFi(ssid)
256 if options.wep_hex != None:
257 c = settings.Wep(ssid, "", options.wep_hex)
258 if options.wep_pass != None:
259 c = settings.Wep(ssid, options.wep_pass)
260 if options.wpa_psk_hex != None:
261 c = settings.WpaPsk(ssid, "", options.wpa_psk_hex)
262 if options.wpa_pass != None:
263 c = settings.WpaPsk(ssid, options.wpa_pass)
264 if c == None:
265 print "Error, connection settings not specified"
266 sys.exit(1)
268 svc = USER_SERVICE
269 svc_conn = us.addCon(c.conmap)
270 hint = svc_conn.settings["connection"]["type"]
271 dev = get_device("", hint)
272 appath = "/"
273 nm._connect_to_signal("StateChanged", print_state_changed)
274 # must be async because ourselves are providing the service
275 dummy_handler = lambda *args: None
276 nm.ActivateConnection(svc, svc_conn, dev, appath,
277 reply_handler=dummy_handler,
278 error_handler=dummy_handler)
279 LOOP = True
281 if options.activate_connection != None:
282 (svc, conpath, devpath, appath) = options.activate_connection.split(',')
283 if svc == "" or svc == "user":
284 svc = USER_SERVICE
285 elif svc == "system":
286 svc = SYSTEM_SERVICE
288 conn = get_connection(svc, conpath)
289 hint = get_connection_devtype(conn)
290 dev = get_device(devpath, hint)
291 if appath == "":
292 appath = "/"
293 nm._connect_to_signal("StateChanged", print_state_changed)
294 # TODO make it accept both objects and opaths
295 nm.ActivateConnection(svc, conn, dev, appath)
296 # TODO (optionally) block only until a stable state is reached
297 LOOP = True
300 ######## demo ##########
302 from dbusclient import DBusMio
303 mio = DBusMio(dbus.SystemBus(), "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
304 i = mio.Introspect()
305 d = mio.GetDevices()
307 if options.demo:
308 nm = NetworkManager()
310 # TODO: generic signal (adapt cnm monitor), print name and args
312 nm["WirelessEnabled"] = "yes"
314 devs = nm.GetDevices()
316 for d in devs:
317 print "\n DEVICE"
318 # TODO: find API for any object
319 d._connect_to_signal("StateChanged", print_state_changed)
321 LOOP = True
322 ######## demo end ##########
324 # TODO wrap this
325 if LOOP:
326 try:
327 print "Entering mainloop"
328 loop.run()
329 except KeyboardInterrupt:
330 print "Loop exited"