2 # cnetworkmanager: Command Line Interface for NetworkManager
3 # by: http://en.opensuse.org/User:Mvidner
4 # license: http://creativecommons.org/licenses/by/3.0/
7 print "cnetworkmanager %s - Command Line Interface for NetworkManager" % VERSION
13 from dbus
.mainloop
.glib
import DBusGMainLoop
14 DBusGMainLoop(set_as_default
=True)
24 bus
= dbus
.SystemBus()
26 # FOOC = connection (service) string
27 # FOOI = interface string
30 # foopi = property interface
31 NMC
= 'org.freedesktop.NetworkManager'
33 PI
= 'org.freedesktop.DBus.Properties'
34 SSC
= "org.freedesktop.NetworkManagerSystemSettings"
35 USC
= "org.freedesktop.NetworkManagerUserSettings"
39 ii
= dbus
.Interface(obj
, 'org.freedesktop.DBus.Introspectable')
42 # TODO: pull them from introspection.xml
43 NM_STATE
= ["UNKNOWN", "ASLEEP", "CONNECTING", "CONNECTED", "DISCONNECTED",]
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)
57 devices
= nmi
.GetDevices()
58 for device
in devices
:
63 print "Active Connections"
64 aconns
= nmpi
.Get(NMI
, "ActiveConnections")
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()]
82 devices
= nmi
.getDevices()
83 for device
in devices
:
85 dump_device_06(device
)
88 print "Active Connections"
90 print nmi
.GetActiveConnections()
92 def bitmask_str(map, value
):
94 for mask
, s
in map.iteritems():
95 if value
& mask
: ret
.append(s
)
98 NM_DEVICE_CAP
= {1: "NM_SUPPORTED", 2: "CARRIER_DETECT",}
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
)
125 devi
= dbus
.Interface(devo
, NMI
+ ".Device")
126 print " Driver:", devi
.getDriver()
127 props
= devi
.getProperties()
128 # TODO: annotate struct
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
))
152 print " Access Points"
153 wdevi
= dbus
.Interface(devo
, NMI
+ ".Device.Wireless")
154 aps
= wdevi
.GetAccessPoints()
157 dump_access_point(ap
)
159 NM_802_11_AP_FLAGS
= {1: "PRIVACY",}
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",}
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()
191 dump_settings_conn(svc
, conn
)
193 def dump_settings_dict(settings
):
194 for n1
, v1
in settings
.iteritems():
196 for n2
, v2
in v1
.iteritems():
197 print " %s: %s" % (n2
, v2
)
199 def dump_settings_conn(svc
, 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')
208 secrets
= si
.GetSecrets("802-11-wireless-security",[],False)
209 dump_settings_dict(secrets
)
212 loop
= gobject
.MainLoop()
222 if arg
== "-h" or arg
== "--help":
226 elif arg
== "-d" or arg
== "--dev":
228 elif arg
== "-c" or arg
== "--actcon":
230 elif arg
== "-u" or arg
== "--usrcon":
232 elif arg
== "-s" or arg
== "--syscon":
234 elif arg
== "-a" or arg
== "--ap":
236 DUMP_DEVS
= True # aps are under wifi devs
237 elif arg
== "--loop":
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"
250 # TODO autodetect (try getDevices?)
252 dump_nm_06('/org/freedesktop/NetworkManager')
254 dump_nm('/org/freedesktop/NetworkManager')
257 print "SYSTEM Connections"
258 sso
= bus
.get_object(SSC
, '/org/freedesktop/NetworkManagerSettings')
259 dump_settings(SSC
, sso
)
262 print "USER Connections"
263 uso
= bus
.get_object(USC
, '/org/freedesktop/NetworkManagerSettings')
264 dump_settings(USC
, uso
)