@@ -12,11 +12,15 @@ Actions:
12
12
deactivate - Deactivate a connection
13
13
offline - Deactivate all connections
14
14
enable - Enable specific connection types
15
- disable - Disable specific connection types"""
15
+ disable - Disable specific connection types
16
+ info - Information about a connection"""
16
17
18
+ import datetime
17
19
from dbus .exceptions import DBusException
18
20
import NetworkManager
19
21
import optparse
22
+ import socket
23
+ import struct
20
24
import sys
21
25
22
26
def main ():
@@ -49,6 +53,12 @@ def main():
49
53
elif args [0 ] == 'disable' :
50
54
disable (args [1 :])
51
55
56
+ elif args [0 ] == 'info' :
57
+ info (args [1 :])
58
+
59
+ elif args [0 ] == 'dump' :
60
+ dump (args [1 :])
61
+
52
62
else :
53
63
p .print_help ()
54
64
sys .exit (1 )
@@ -141,5 +151,67 @@ def disable(names):
141
151
sys .exit (1 )
142
152
setattr (NetworkManager .NetworkManager , n .title () + 'Enabled' , False )
143
153
154
+ def info (names ):
155
+ connections = [x .GetSettings () for x in NetworkManager .Settings .ListConnections ()]
156
+ connections = dict ([(x ['connection' ]['id' ], x ) for x in connections ])
157
+
158
+ for n in names :
159
+ if n not in connections :
160
+ print >> sys .stderr , "No such connection: %s" % n
161
+
162
+ line = "Info about '%s'" % n
163
+ print line + "\n " + '=' * len (line )
164
+ conn = connections [n ]
165
+ print "Type:" , conn ['connection' ]['type' ]
166
+ print "Connect automatically:" , ["No" ,"Yes" ][conn ['connection' ].get ('autoconnect' , True )]
167
+ print "Last connected on:" , str (datetime .datetime .fromtimestamp (conn ['connection' ]['timestamp' ]))
168
+ print "IPv4 settings (%s)" % conn ['ipv4' ]['method' ]
169
+ print " Address(es):" , auto (ipv4s (conn ['ipv4' ]['addresses' ]))
170
+ print " DNS servers:" , auto (ipv4s (conn ['ipv4' ]['dns' ]))
171
+ print " Routes:" , auto (routes (conn ['ipv4' ]['routes' ]))
172
+ print " Can be default route:" , ["Yes" ,"No" ][conn ['ipv4' ].get ('never-default' , False )]
173
+
174
+ if conn ['connection' ]['type' ] == '802-3-ethernet' :
175
+ print "Physical link"
176
+ print " MAC address:" , mac (conn ['802-3-ethernet' ].get ('mac-address' ))
177
+ elif conn ['connection' ]['type' ] == '802-11-wireless' :
178
+ print "Wireless link"
179
+ print " MAC address:" , mac (conn ['802-11-wireless' ].get ('mac-address' ))
180
+ print " SSID:" , ssid (conn ['802-11-wireless' ]['ssid' ])
181
+ print " Wireless security:" , conn [conn ['802-11-wireless' ]['security' ]]['key-mgmt' ]
182
+ elif conn ['connection' ]['type' ] == 'vpn' :
183
+ print "VPN"
184
+ print " Type:" , conn ['vpn' ]['service-type' ].rsplit ('.' ,1 )[- 1 ]
185
+ print " Remote:" , conn ['vpn' ]['data' ]['remote' ]
186
+
187
+ def dump (names ):
188
+ connections = [x .GetSettings () for x in NetworkManager .Settings .ListConnections ()]
189
+ connections = dict ([(x ['connection' ]['id' ], x ) for x in connections ])
190
+
191
+ for n in names :
192
+ if n not in connections :
193
+ print >> sys .stderr , "No such connection: %s" % n
194
+
195
+ from pprint import pprint
196
+ pprint (connections [n ])
197
+
198
+ def auto (seq ):
199
+ return "(Automatic)" if not seq else ", " .join (seq )
200
+
201
+ def ipv4 (ip ):
202
+ return socket .inet_ntoa (struct .pack ('L' ,ip ))
203
+
204
+ def ipv4s (seq ):
205
+ return [ipv4 (x ) for x in seq ]
206
+
207
+ def routes (seq ):
208
+ return ["%s/%d -> %s" % (ipv4 (x [0 ]), x [1 ], ipv4 (x [2 ])) for x in seq ]
209
+
210
+ def mac (seq ):
211
+ return "(Automatic)" if not seq else "%X:%X:%X:%X:%X:%X" % tuple (seq )
212
+
213
+ def ssid (seq ):
214
+ return "" .join ([chr (x ) for x in seq ])
215
+
144
216
if __name__ == '__main__' :
145
217
main ()
0 commit comments