Skip to content

Commit 4ce1fce

Browse files
committed
Add the n-m tool and call this 0.9.2
1 parent 879d1b3 commit 4ce1fce

File tree

5 files changed

+129
-2
lines changed

5 files changed

+129
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
build
2+
debian
13
dist
24
docs/_build
35
.*.sw?

docs/conf.py

+1-1
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.1'
53+
release = '0.9.2'
5454

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

docs/index.rst

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ calls are forwarded to the correct interface.
99
I wrote it to reduce a 100-line python script to 50 lines. Not realizing that
1010
the library has more lines than the ones I removed. Oh well 😊
1111

12+
As of version 0.9.2, python-networkmanager also ships a command-line utility
13+
called n-m, which allows you to manipulate NetworkManager's state from the
14+
command line.
15+
1216
:mod:`NetworkManager` -- Easy communication with NetworkManager
1317
---------------------------------------------------------------
1418
.. module:: NetworkManager
@@ -126,3 +130,17 @@ interface.
126130
.. toctree::
127131
:maxdepth: 2
128132

133+
The n-m utility
134+
---------------
135+
n-m is a command-line tool to deal with network-manager. It can connect you to
136+
defined networks and disconnect you again.
137+
138+
Usage: [options] action [arguments]
139+
140+
Actions:
141+
list - List all defined and active connections
142+
activate - Activate a connection
143+
deactivate - Deactivate a connection
144+
offline - Deactivate all connections
145+
146+
Suggestions for more functionality for this tool are welcome!

n-m

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/python
2+
"""
3+
n-m is a command-line tool to deal with network-manager. It can connect you to
4+
defined networks and disconnect you again.
5+
"""
6+
7+
usage = """%prog [options] action [arguments]
8+
9+
Actions:
10+
list - List all defined and active connections
11+
activate - Activate a connection
12+
deactivate - Deactivate a connection
13+
offline - Deactivate all connections"""
14+
15+
import NetworkManager
16+
import optparse
17+
import sys
18+
19+
def main():
20+
p = optparse.OptionParser(usage=usage)
21+
opts, args = p.parse_args()
22+
23+
if not args:
24+
p.print_help()
25+
sys.exit(1)
26+
27+
if args[0] == 'list':
28+
list_()
29+
30+
elif args[0] == 'offline':
31+
offline()
32+
33+
elif len(args) < 2:
34+
p.print_help()
35+
sys.exit(1)
36+
37+
elif args[0] == 'activate':
38+
activate(args[1:])
39+
40+
elif args[0] == 'deactivate':
41+
deactivate(args[1:])
42+
43+
def list_():
44+
active = [x.Connection.GetSettings()['connection']['id']
45+
for x in NetworkManager.NetworkManager.ActiveConnections]
46+
connections = [x.GetSettings()['connection']['id']
47+
for x in NetworkManager.Settings.ListConnections()]
48+
for conn in sorted(connections):
49+
prefix = '* ' if conn in active else ' '
50+
print prefix + conn
51+
52+
def activate(names):
53+
connections = NetworkManager.Settings.ListConnections()
54+
connections = dict([(x.GetSettings()['connection']['id'], x) for x in connections])
55+
56+
if not NetworkManager.NetworkManager.NetworkingEnabled:
57+
NetworkManager.NetworkManager.Enable(True)
58+
for n in names:
59+
if n not in connections:
60+
print >>sys.stderr, "No such connection: %s" % n
61+
sys.exit(1)
62+
63+
print "Activating connection '%s'" % n
64+
conn = connections[n]
65+
ctype = conn.GetSettings()['connection']['type']
66+
if ctype == 'vpn':
67+
for dev in NetworkManager.NetworkManager.GetDevices():
68+
if dev.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED and dev.Managed:
69+
break
70+
else:
71+
print >>sys.stderr, "No active, managed device found"
72+
sys.exit(1)
73+
else:
74+
dtype = {
75+
'802-11-wireless': NetworkManager.NM_DEVICE_TYPE_WIFI,
76+
'802-3-ethernet': NetworkManager.NM_DEVICE_TYPE_ETHERNET,
77+
'gsm': NetworkManager.NM_DEVICE_TYPE_MODEM,
78+
}.get(ctype,ctype)
79+
devices = NetworkManager.NetworkManager.GetDevices()
80+
81+
for dev in devices:
82+
if dev.DeviceType == dtype and dev.State == NetworkManager.NM_DEVICE_STATE_DISCONNECTED:
83+
break
84+
else:
85+
print >>sys.stderr, "No suitable and available %s device found" % ctype
86+
sys.exit(1)
87+
88+
NetworkManager.NetworkManager.ActivateConnection(conn, dev, "/")
89+
90+
def deactivate(names):
91+
active = NetworkManager.NetworkManager.ActiveConnections
92+
active = dict([(x.Connection.GetSettings()['connection']['id'], x) for x in active])
93+
94+
for n in names:
95+
if n not in active:
96+
print >>sys.stderr, "No such connection: %s" % n
97+
sys.exit(1)
98+
99+
print "Deactivating connection '%s'" % n
100+
NetworkManager.NetworkManager.DeactivateConnection(active[n])
101+
102+
def offline():
103+
NetworkManager.NetworkManager.Enable(False)
104+
105+
if __name__ == '__main__':
106+
main()

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from distutils.core import setup
44

55
setup(name = "python-networkmanager",
6-
version = "0.9.1",
6+
version = "0.9.2",
77
author = "Dennis Kaarsemaker",
88
author_email = "[email protected]",
99
url = "http://github.com/seveas/python-networkmanager",
1010
description = "Easy communication with NetworkManager",
1111
py_modules = ["NetworkManager"],
12+
scripts = ["n-m"],
1213
classifiers = [
1314
'Development Status :: 5 - Production/Stable',
1415
'Intended Audience :: Developers',

0 commit comments

Comments
 (0)