File: activate_connection.py

package info (click to toggle)
python-networkmanager 2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 336 kB
  • sloc: python: 1,713; makefile: 116
file content (39 lines) | stat: -rw-r--r-- 1,209 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Activate a connection by name
"""

import NetworkManager
import sys

# Find the connection
name = sys.argv[1]
connections = NetworkManager.Settings.ListConnections()
connections = dict([(x.GetSettings()['connection']['id'], x) for x in connections])
conn = connections[name]

# Find a suitable device
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("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("No suitable and available %s device found" % ctype)
        sys.exit(1)

# And connect
NetworkManager.NetworkManager.ActivateConnection(conn, dev, "/")