Permalink
Browse files

Add the n-m tool and call this 0.9.2

  • Loading branch information...
1 parent 879d1b3 commit 4ce1fce2d19d4082b10280d60f0e40fa3668abba @seveas committed Nov 26, 2011
Showing with 129 additions and 2 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 docs/conf.py
  3. +18 −0 docs/index.rst
  4. +106 −0 n-m
  5. +2 −1 setup.py
View
@@ -1,3 +1,5 @@
+build
+debian
dist
docs/_build
.*.sw?
View
@@ -50,7 +50,7 @@
# The short X.Y version.
version = '0.9'
# The full version, including alpha/beta/rc tags.
-release = '0.9.1'
+release = '0.9.2'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
View
@@ -9,6 +9,10 @@ calls are forwarded to the correct interface.
I wrote it to reduce a 100-line python script to 50 lines. Not realizing that
the library has more lines than the ones I removed. Oh well 😊
+As of version 0.9.2, python-networkmanager also ships a command-line utility
+called n-m, which allows you to manipulate NetworkManager's state from the
+command line.
+
:mod:`NetworkManager` -- Easy communication with NetworkManager
---------------------------------------------------------------
.. module:: NetworkManager
@@ -126,3 +130,17 @@ interface.
.. toctree::
:maxdepth: 2
+The n-m utility
+---------------
+n-m is a command-line tool to deal with network-manager. It can connect you to
+defined networks and disconnect you again.
+
+Usage: [options] action [arguments]
+
+Actions:
+ list - List all defined and active connections
+ activate - Activate a connection
+ deactivate - Deactivate a connection
+ offline - Deactivate all connections
+
+Suggestions for more functionality for this tool are welcome!
View
106 n-m
@@ -0,0 +1,106 @@
+#!/usr/bin/python
+"""
+n-m is a command-line tool to deal with network-manager. It can connect you to
+defined networks and disconnect you again.
+"""
+
+usage = """%prog [options] action [arguments]
+
+Actions:
+ list - List all defined and active connections
+ activate - Activate a connection
+ deactivate - Deactivate a connection
+ offline - Deactivate all connections"""
+
+import NetworkManager
+import optparse
+import sys
+
+def main():
+ p = optparse.OptionParser(usage=usage)
+ opts, args = p.parse_args()
+
+ if not args:
+ p.print_help()
+ sys.exit(1)
+
+ if args[0] == 'list':
+ list_()
+
+ elif args[0] == 'offline':
+ offline()
+
+ elif len(args) < 2:
+ p.print_help()
+ sys.exit(1)
+
+ elif args[0] == 'activate':
+ activate(args[1:])
+
+ elif args[0] == 'deactivate':
+ deactivate(args[1:])
+
+def list_():
+ active = [x.Connection.GetSettings()['connection']['id']
+ for x in NetworkManager.NetworkManager.ActiveConnections]
+ connections = [x.GetSettings()['connection']['id']
+ for x in NetworkManager.Settings.ListConnections()]
+ for conn in sorted(connections):
+ prefix = '* ' if conn in active else ' '
+ print prefix + conn
+
+def activate(names):
+ connections = NetworkManager.Settings.ListConnections()
+ connections = dict([(x.GetSettings()['connection']['id'], x) for x in connections])
+
+ if not NetworkManager.NetworkManager.NetworkingEnabled:
+ NetworkManager.NetworkManager.Enable(True)
+ for n in names:
+ if n not in connections:
+ print >>sys.stderr, "No such connection: %s" % n
+ sys.exit(1)
+
+ print "Activating connection '%s'" % n
+ conn = connections[n]
+ ctype = conn.GetSettings()['connection']['type']
+ if ctype == 'vpn':
+ for dev in NetworkManager.NetworkManager.GetDevices():
+ if dev.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED and dev.Managed:
+ break
+ else:
+ print >>sys.stderr, "No active, managed device found"
+ sys.exit(1)
+ else:
+ dtype = {
+ '802-11-wireless': NetworkManager.NM_DEVICE_TYPE_WIFI,
+ '802-3-ethernet': NetworkManager.NM_DEVICE_TYPE_ETHERNET,
+ 'gsm': NetworkManager.NM_DEVICE_TYPE_MODEM,
+ }.get(ctype,ctype)
+ devices = NetworkManager.NetworkManager.GetDevices()
+
+ for dev in devices:
+ if dev.DeviceType == dtype and dev.State == NetworkManager.NM_DEVICE_STATE_DISCONNECTED:
+ break
+ else:
+ print >>sys.stderr, "No suitable and available %s device found" % ctype
+ sys.exit(1)
+
+ NetworkManager.NetworkManager.ActivateConnection(conn, dev, "/")
+
+def deactivate(names):
+ active = NetworkManager.NetworkManager.ActiveConnections
+ active = dict([(x.Connection.GetSettings()['connection']['id'], x) for x in active])
+
+ for n in names:
+ if n not in active:
+ print >>sys.stderr, "No such connection: %s" % n
+ sys.exit(1)
+
+ print "Deactivating connection '%s'" % n
+ NetworkManager.NetworkManager.DeactivateConnection(active[n])
+
+def offline():
+ NetworkManager.NetworkManager.Enable(False)
+
+if __name__ == '__main__':
+ main()
View
@@ -3,12 +3,13 @@
from distutils.core import setup
setup(name = "python-networkmanager",
- version = "0.9.1",
+ version = "0.9.2",
author = "Dennis Kaarsemaker",
author_email = "dennis@kaarsemaker.net",
url = "http://github.com/seveas/python-networkmanager",
description = "Easy communication with NetworkManager",
py_modules = ["NetworkManager"],
+ scripts = ["n-m"],
classifiers = [
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',

0 comments on commit 4ce1fce

Please sign in to comment.