Skip to content

Commit e955aba

Browse files
committed
Add some informational commands
1 parent a49b9d1 commit e955aba

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# The short X.Y version.
5151
version = '0.9'
5252
# The full version, including alpha/beta/rc tags.
53-
release = '0.9.3'
53+
release = '0.9.5'
5454

5555
# The language for content autogenerated by Sphinx. Refer to documentation
5656
# for a list of supported languages.

docs/index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,6 @@ Actions:
142142
activate - Activate a connection
143143
deactivate - Deactivate a connection
144144
offline - Deactivate all connections
145-
146-
Suggestions for more functionality for this tool are welcome!
145+
enable - Enable specific connection types
146+
disable - Disable specific connection types
147+
info - Information about a connection

n-m

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ Actions:
1212
deactivate - Deactivate a connection
1313
offline - Deactivate all connections
1414
enable - Enable specific connection types
15-
disable - Disable specific connection types"""
15+
disable - Disable specific connection types
16+
info - Information about a connection"""
1617

18+
import datetime
1719
from dbus.exceptions import DBusException
1820
import NetworkManager
1921
import optparse
22+
import socket
23+
import struct
2024
import sys
2125

2226
def main():
@@ -49,6 +53,12 @@ def main():
4953
elif args[0] == 'disable':
5054
disable(args[1:])
5155

56+
elif args[0] == 'info':
57+
info(args[1:])
58+
59+
elif args[0] == 'dump':
60+
dump(args[1:])
61+
5262
else:
5363
p.print_help()
5464
sys.exit(1)
@@ -141,5 +151,67 @@ def disable(names):
141151
sys.exit(1)
142152
setattr(NetworkManager.NetworkManager, n.title() + 'Enabled', False)
143153

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+
144216
if __name__ == '__main__':
145217
main()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from distutils.core import setup
44

55
setup(name = "python-networkmanager",
6-
version = "0.9.3",
6+
version = "0.9.5",
77
author = "Dennis Kaarsemaker",
88
author_email = "dennis@kaarsemaker.net",
99
url = "http://github.com/seveas/python-networkmanager",

0 commit comments

Comments
 (0)