summaryrefslogtreecommitdiff
path: root/bin
diff options
authorJonathan Cave <jonathan.cave@canonical.com>2019-10-23 17:09:56 +0100
committerJonathan Cave <jonathan.cave@canonical.com>2019-10-23 17:09:56 +0100
commit4179ff190eddfa4e2c4a01082f26f972dc4fee7b (patch)
tree97c5c19e69c76a2759d73248464b950d22b0bbe6 /bin
parent2602516963f9a6a1418ff7d217effe8f3fad2190 (diff)
wifi_nmcli_backup.py: better keyfile detection
These changes were intially motivated by the need to handle SSID names containing path separator characters. Use the most appopriate method depending on NM version.
Diffstat (limited to 'bin')
-rwxr-xr-xbin/wifi_nmcli_backup.py63
1 files changed, 45 insertions, 18 deletions
diff --git a/bin/wifi_nmcli_backup.py b/bin/wifi_nmcli_backup.py
index 7ec9c892..6e4db6a8 100755
--- a/bin/wifi_nmcli_backup.py
+++ b/bin/wifi_nmcli_backup.py
@@ -12,20 +12,48 @@ import shutil
import subprocess as sp
import sys
+from distutils.version import LooseVersion
+
NM_CON_DIR = '/etc/NetworkManager/system-connections'
SAVE_DIR = os.path.join(os.path.expandvars(
'$PLAINBOX_SESSION_SHARE'), 'stored-system-connections')
-def get_nm_connections():
- c = []
- cmd = 'nmcli -t -f TYPE,NAME c'
+# versions of NM in cosmic or later allow you to request the keyfile name
+# in `nmcli -t -f <FIELDS> c` output used below
+def legacy_nmcli():
+ cmd = "nmcli -v"
output = sp.check_output(cmd, shell=True)
- for line in output.decode(sys.stdout.encoding).splitlines():
- con_type, name = line.strip().split(':')
- if con_type == '802-11-wireless':
- c.append(name)
- return c
+ version = LooseVersion(output.strip().split()[-1].decode())
+ # check if using an earlier nmcli version with different api
+ # nmcli in cosmic is 1.12.4, bionic is 1.10
+ if version < LooseVersion("1.12.0"):
+ return True
+ return False
+
+
+# Creation of keyfile names can be found in:
+# https://gitlab.freedesktop.org/NetworkManager/NetworkManager/blob/master/libnm-core/nm-keyfile.c#L4046
+# Old format is to replace path separators with '*', in versions that use
+# the new format we can just use the filename supplied by nmcli
+def get_nm_keyfiles():
+ filenames = []
+ if legacy_nmcli():
+ cmd = 'nmcli -t -f TYPE,NAME c'
+ output = sp.check_output(cmd, shell=True)
+ for line in output.decode(sys.stdout.encoding).splitlines():
+ con_type, name = line.strip().split(':')
+ if con_type == '802-11-wireless':
+ filename = name.replace('/', '*')
+ filenames.append(os.path.join(NM_CON_DIR, filename))
+ else:
+ cmd = 'nmcli -t -f TYPE,FILENAME c'
+ output = sp.check_output(cmd, shell=True)
+ for line in output.decode(sys.stdout.encoding).splitlines():
+ con_type, filename = line.strip().split(':')
+ if con_type == '802-11-wireless':
+ filenames.append(filename)
+ return filenames
def reload_nm_connections():
@@ -33,20 +61,19 @@ def reload_nm_connections():
sp.check_call(cmd, shell=True)
-def save_connections(con_list):
- if len(con_list) == 0:
+def save_connections(keyfile_list):
+ if len(keyfile_list) == 0:
print('No stored 802.11 connections to save')
return
if not os.path.exists(SAVE_DIR):
os.makedirs(SAVE_DIR)
- for c in con_list:
- print('Save connection {}'.format(c))
- c_loc = os.path.join(NM_CON_DIR, c)
- if not os.path.exists(c_loc):
- print(' No stored connection fount at {}'.format(c_loc))
+ for f in keyfile_list:
+ print('Save connection {}'.format(f))
+ if not os.path.exists(f):
+ print(' No stored connection fount at {}'.format(f))
continue
- print(' Found file {}'.format(c_loc))
- save_f = shutil.copy(c_loc, SAVE_DIR)
+ print(' Found file {}'.format(f))
+ save_f = shutil.copy(f, SAVE_DIR)
print(' Saved copy at {}'.format(save_f))
@@ -71,7 +98,7 @@ if __name__ == '__main__':
action = sys.argv[1]
if action == 'save':
- save_connections(get_nm_connections())
+ save_connections(get_nm_keyfiles())
elif action == 'restore':
restore_connections()
reload_nm_connections()