feddb5fcfaeb2da85f68649b641ebe6968586e61
[platform/upstream/connman.git] / test / vpn-property
1 #!/usr/bin/python
2
3 import sys
4 import dbus
5
6 def make_variant(string):
7         return dbus.String(string, variant_level=1)
8
9 def extract_values(values):
10         val = "{"
11         for key in list(values.keys()):
12                 val += " " + key + "="
13                 if key in ["Servers", "Excludes"]:
14                         val += extract_list(values[key])
15                 else:
16                         val += str(values[key])
17         val += " }"
18         return val
19
20 def extract_list(list):
21         val = "["
22         for i in list:
23                 if type(i).__name__ == 'Dictionary':
24                         val += extract_values(i)
25                 elif type(i).__name__ == 'Struct':
26                         val += extract_list(i)
27                 else:
28                         val += " " + str(i)
29         val += "]"
30         return val
31
32 argc = len(sys.argv)
33
34 if (argc < 2):
35         print("Usage: %s <VPN connection id> [<property name>] [<property values>]" % (sys.argv[0]))
36         sys.exit(1)
37
38 bus = dbus.SystemBus()
39
40 manager = dbus.Interface(bus.get_object("net.connman.vpn", "/"),
41                                         "net.connman.vpn.Manager")
42
43 connections = manager.GetConnections()
44
45 path = "/net/connman/vpn/connection/" + sys.argv[1]
46
47 print("Attempting to connect VPN %s" % (path))
48
49 connection = dbus.Interface(bus.get_object("net.connman.vpn", path),
50                                             "net.connman.vpn.Connection")
51
52 if (argc < 3):
53         properties = connection.GetProperties()
54         for key in list(properties.keys()):
55                 if key in ["IPv4", "IPv6" ]:
56                         val = extract_values(properties[key])
57                 elif key in ["Nameservers","ServerRoutes","UserRoutes"]:
58                         val = extract_list(properties[key])
59                 else:
60                         val = str(properties[key])
61                 print("    %s = %s" % (key, val))
62
63 elif (argc < 4):
64         try:
65                 connection.ClearProperty(sys.argv[2])
66         except dbus.DBusException as error:
67                 print("%s: %s" % (error._dbus_error_name, error.message))
68 else:
69         try:
70                 connection.SetProperty(sys.argv[2], sys.argv[3])
71         except dbus.DBusException as error:
72                 print("%s: %s" % (error._dbus_error_name, error.message))