simple-agent (3365B)
1 #!/usr/bin/python3 2 3 from gi.repository import GLib 4 5 import dbus 6 import dbus.service 7 import dbus.mainloop.glib 8 import sys 9 from random import randrange 10 11 class Canceled(dbus.DBusException): 12 _dbus_error_name = "net.connman.iwd.Error.Canceled" 13 14 class Agent(dbus.service.Object): 15 passphrase = None 16 17 @dbus.service.method("net.connman.iwd.Agent", 18 in_signature='', out_signature='') 19 def Release(self): 20 print("Release") 21 mainloop.quit() 22 23 @dbus.service.method("net.connman.iwd.Agent", 24 in_signature='o', 25 out_signature='s') 26 def RequestPassphrase(self, path): 27 print("RequestPassphrase (%s)" % (path)) 28 29 print("Service credentials requested, type cancel to cancel") 30 passphrase = input('Answer: ') 31 32 if not passphrase or passphrase == 'cancel': 33 raise Canceled("canceled") 34 35 print("returning (%s)" % (passphrase)) 36 return passphrase 37 38 @dbus.service.method("net.connman.iwd.Agent", 39 in_signature='o', 40 out_signature='s') 41 def RequestPrivateKeyPassphrase(self, path): 42 print("RequestPrivateKeyPassphrase (%s)" % (path)) 43 44 print("Service credentials requested, type cancel to cancel") 45 passphrase = input('Answer: ') 46 47 if not passphrase or passphrase == 'cancel': 48 raise Canceled("canceled") 49 50 print("returning (%s)" % (passphrase)) 51 return passphrase 52 53 @dbus.service.method("net.connman.iwd.Agent", 54 in_signature='o', 55 out_signature='ss') 56 def RequestUserNameAndPassword(self, path): 57 print("RequestUserNameAndPassword (%s)" % (path)) 58 59 print("Service credentials requested, type cancel to cancel") 60 user = input('User name: ') 61 62 if not user or user == 'cancel': 63 raise Canceled("canceled") 64 65 passwd = input('User password: ') 66 67 if not passwd or passwd == 'cancel': 68 raise Canceled("canceled") 69 70 print("returning (%s, %s)" % (user, passwd)) 71 return (user, passwd) 72 73 @dbus.service.method("net.connman.iwd.Agent", 74 in_signature='s', out_signature='') 75 def Cancel(self, reason): 76 print("Cancel: " + reason) 77 78 def print_usage(): 79 print("Usage:") 80 print("For WPA input:") 81 print("%s Passphrase=<passphrase>" % (sys.argv[0])) 82 print("Help: %s help" % (sys.argv[0])) 83 sys.exit(1) 84 85 if __name__ == '__main__': 86 if len(sys.argv) == 2 and sys.argv[1] == "help": 87 print_usage() 88 89 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 90 91 bus = dbus.SystemBus() 92 manager = dbus.Interface(bus.get_object('net.connman.iwd', 93 '/net/connman/iwd'), 94 'net.connman.iwd.AgentManager') 95 96 path = "/test/agent/" + str(randrange(100)) 97 object = Agent(bus, path) 98 99 if len(sys.argv) >= 2: 100 for arg in sys.argv[1:]: 101 if arg.startswith("Passphrase="): 102 object.passphrase = arg.replace("Passphrase=", "", 1) 103 else: 104 print_usage() 105 106 try: 107 manager.RegisterAgent(path) 108 except: 109 print("Cannot register iwd agent.") 110 111 mainloop = GLib.MainLoop() 112 mainloop.run()