Skip to content

Instantly share code, notes, and snippets.

@asmoore82
Created May 1, 2019 14:48
Show Gist options
  • Save asmoore82/8bb2b15ca6c874fed9d7f3aad4d4c140 to your computer and use it in GitHub Desktop.
Save asmoore82/8bb2b15ca6c874fed9d7f3aad4d4c140 to your computer and use it in GitHub Desktop.
Python script to call FileWave Admin and export all clients to flat JSON list
#Copyright (c) 2018 Alamance-Burlington Schools, MIT License
import json
import os
import subprocess
#This script will safely do nothing
# when fw_host and scp_host values remain empty
# and/or if FileWaveAdmin.exe is not found as expected
#Connection Info
# - note that this account will get booted from GUI when script runs
#fw_host = 'your-fw-host'
fw_host = ''
fw_username = 'your-fw-script-account'
fw_password = 'your-fw-script-password'
#Iterable of groups to ignore
# - note that the full path of the groups must be specified
fw_skip_groups = ('/LDAP Groups',)
#Full path of FileWaveAdmin
# - we're cheating here on the OS directory separator
fw_admin = '/Program Files (x86)/FileWave/FileWaveAdmin.exe'
fw_list_clients_args = [fw_admin,
'-H', fw_host,
'-u', fw_username,
'-p', fw_password,
'--listClients']
#Files to create - fwo = FileWave Output
fwo_clients = 'all-clients.json'
fwo_min = 'all-clients.min.json'
fwo_flat = 'all-clients.flat.json'
#Optional path to secure copy the final results to
# - note that pscp.exe from Simon Tatham must be in your PATH
#scp_path = 'user@your-scp-server:/scp/path'
scp_path = ''
scp_password = 'secret'
scp_args = ['pscp.exe', '-batch',
'-pw', scp_password,
fwo_flat,
scp_path]
#A recursive generator to "walk" the JSON tree add build pretty pathnames
# - marks a group with a trailing /
# - ^ as a happy side-effect of that,
# the nameless root group becomes a leading / on all paths
# - marks a clone with a trailing @
def walker(j, path=''):
if isinstance(j, list):
for i in j:
for x in walker(i, path):
yield x
elif isinstance(j, dict):
t = j.get('type')
n = j.get('name')
if n:
n = '{}/{}'.format(path, n)
if t == 'client':
yield n
elif t == 'clone':
yield '{}@'.format(n)
elif t == 'group':
yield '{}/'.format(n)
if n not in fw_skip_groups:
for x in walker(j.get('children'), n):
yield x
if __name__ == "__main__":
if fw_host and os.path.isfile(fw_admin):
with open(fwo_clients, 'w') as f:
subprocess.check_call(fw_list_clients_args, stdout=f)
if os.path.isfile(fwo_clients):
with open(fwo_clients, 'r') as f:
j = json.load(f)
with open(fwo_min, 'w') as f:
json.dump(j, f)
j = list(walker(j))
with open(fwo_flat, 'w') as f:
json.dump(j, f)
if scp_path:
subprocess.check_call(scp_args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment