summaryrefslogtreecommitdiff
path: root/bin
diff options
authorJonathan Cave <jonathan.cave@canonical.com>2019-02-07 10:58:54 +0000
committerJonathan Cave <jonathan.cave@canonical.com>2019-02-07 10:58:54 +0000
commit18d7bfa9d0151c323d18db54abd53717ce2dcd67 (patch)
treed72d557bc55fe8f76c7f0575382a0e1ce4ec2704 /bin
parent3f3d3803b15960ad38e22895cebbd203f68f23f0 (diff)
wireless: new NM connection save/restore jobs
Diffstat (limited to 'bin')
-rwxr-xr-xbin/wifi_nmcli_backup.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/bin/wifi_nmcli_backup.py b/bin/wifi_nmcli_backup.py
new file mode 100755
index 00000000..7ec9c892
--- /dev/null
+++ b/bin/wifi_nmcli_backup.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+# Copyright 2019 Canonical Ltd.
+# All rights reserved.
+#
+# Written by:
+# Jonathan Cave <jonathan.cave@canonical.com>
+#
+# Save/Restore NetworkManager wifi connections
+
+import os
+import shutil
+import subprocess as sp
+import sys
+
+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'
+ 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
+
+
+def reload_nm_connections():
+ cmd = 'nmcli c reload'
+ sp.check_call(cmd, shell=True)
+
+
+def save_connections(con_list):
+ if len(con_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))
+ continue
+ print(' Found file {}'.format(c_loc))
+ save_f = shutil.copy(c_loc, SAVE_DIR)
+ print(' Saved copy at {}'.format(save_f))
+
+
+def restore_connections():
+ saved_list = [f for f in os.listdir(
+ SAVE_DIR) if os.path.isfile(os.path.join(SAVE_DIR, f))]
+ if len(saved_list) == 0:
+ print('No stored 802.11 connections found')
+ return
+ for f in saved_list:
+ save_f = os.path.join(SAVE_DIR, f)
+ print('Restore connection {}'.format(save_f))
+ restore_f = shutil.copy(save_f, NM_CON_DIR)
+ print(' Restored file at {}'.format(restore_f))
+ os.remove(save_f)
+ print(' Removed copy from {}'.format(save_f))
+
+
+if __name__ == '__main__':
+ if len(sys.argv) != 2:
+ raise SystemExit('ERROR: please specify save or restore')
+ action = sys.argv[1]
+
+ if action == 'save':
+ save_connections(get_nm_connections())
+ elif action == 'restore':
+ restore_connections()
+ reload_nm_connections()
+ else:
+ raise SystemExit('ERROR: unrecognised action')