|
| 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() |
0 commit comments