#!/usr/bin/python import gobject import dbus import dbus.service import dbus.mainloop.glib import sys class Canceled(dbus.DBusException): _dbus_error_name = "net.connman.Error.Canceled" class Agent(dbus.service.Object): identity = None passphrase = None wpspin = None username = None password = None @dbus.service.method("net.connman.Agent", in_signature='', out_signature='') def Release(self): print("Release") mainloop.quit() def input_passphrase(self): response = {} if not self.identity and not self.passphrase and not self.wpspin: args = raw_input('Answer: ') for arg in args.split(): if arg.startswith("Identity="): identity = arg.replace("Identity=", "", 1) response["Identity"] = identity break if arg.startswith("Passphrase="): passphrase = arg.replace("Passphrase=", "", 1) response["Passphrase"] = passphrase break if arg.startswith("WPS="): wpspin = arg.replace("WPS=", "", 1) response["WPS"] = wpspin break else: if self.identity: response["Identity"] = self.identity if self.passphrase: response["Passphrase"] = self.passphrase if self.wpspin: response["WPS"] = self.wpspin return response def input_username(self): response = {} if not self.username and not self.password: args = raw_input('Answer: ') for arg in args.split(): if arg.startswith("Username="): username = arg.replace("Username=", "", 1) response["Username"] = username break if arg.startswith("Password="): password = arg.replace("Password=", "", 1) response["Password"] = password break else: if self.username: response["Username"] = self.username if self.password: response["Password"] = self.password return response @dbus.service.method("net.connman.Agent", in_signature='oa{sv}', out_signature='a{sv}') def RequestInput(self, path, fields): print "RequestInput (%s,%s)" % (path, fields) response = None if fields.has_key("Passphrase"): response = self.input_passphrase() elif fields.has_key("Username"): response = self.input_username() else: print "No method to answer the input request" print "returning (%s)" % (response) return response @dbus.service.method("net.connman.Agent", in_signature='os', out_signature='') def ReportError(self, path, error): print "ReportError %s, %s" % (path, error) retry = raw_input("Retry service (yes/no): ") if (retry == "yes"): class Retry(dbus.DBusException): _dbus_error_name = "net.connman.Agent.Error.Retry" raise Retry("retry service") else: return @dbus.service.method("net.connman.Agent", in_signature='', out_signature='') def Cancel(self): print "Cancel" def print_usage(): print "Usage:" print "For EAP/WPA input:" print "%s Identity= Passphrase= WPS=" % (sys.argv[0]) print "For WISPr login input:" print "%s Username= Password=" % (sys.argv[0]) print "Help: %s help" % (sys.argv[0]) sys.exit(1) if __name__ == '__main__': if len(sys.argv) == 2 and sys.argv[1] == "help": print_usage() dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) bus = dbus.SystemBus() manager = dbus.Interface(bus.get_object('net.connman', "/"), 'net.connman.Manager') path = "/test/agent" object = Agent(bus, path) if len(sys.argv) >= 2: for arg in sys.argv[1:]: if arg.startswith("Identity="): object.identity = arg.replace("Identity=", "", 1) elif arg.startswith("Passphrase="): object.passphrase = arg.replace("Passphrase=", "", 1) elif arg.startswith("WPS="): object.wpspin = arg.replace("WPS=", "", 1) elif arg.startswith("Username="): object.username = arg.replace("Username=", "", 1) elif arg.startswith("Password="): object.password = arg.replace("Password=", "", 1) else: print_usage() manager.RegisterAgent(path) mainloop = gobject.MainLoop() mainloop.run() #manager.UnregisterAgent(path)