Skip to content

Instantly share code, notes, and snippets.

@haircut
Last active September 30, 2020 21:17
Show Gist options
  • Save haircut/108d3b8bd402cc56aa1f65df54f3b7eb to your computer and use it in GitHub Desktop.
Save haircut/108d3b8bd402cc56aa1f65df54f3b7eb to your computer and use it in GitHub Desktop.
NoMAD control scripts

NoMAD control scripts

  • nomad-add-launchagent.py: creates the NoMAD LaunchAgent
  • nomad-load-launchagent.py: loads an existing NoMAD LaunchAgent
  • nomad-pre-update.py: unloads NoMAD LaunchAgent and quits NoMAD prior to installing an updated version

These scripts are designed to be used in Jamf Pro policies. I've separated the functionality for different use cases and flexibility. The ...add... and ...load... file naming convention ensures the scripts will run in the correct order since Jamf Pro runs scripts alphabetically.

Example Jamf Pro policies

Install Policy

  • Package: NoMAD.pkg
  • Script: nomad-add-launchagent.py After
  • Script: nomad-load-launchagent.py After

Update Policy

  • Script: nomad-pre-update.py Before
  • Package: NoMAD.pkg
  • Script: nomad-load-launchagent.py After
#!/usr/bin/python
'''
Install NoMAD LaunchAgent
'''
import os
import sys
import plistlib
def add_launchagent(job):
'''Writes passed LaunchAgent content and sets correct permissions'''
success = True
path = os.path.join('/Library/LaunchAgents/',
'{}.plist'.format(job['Label']))
try:
with open(path, 'w+') as la:
plistlib.writePlist(job, path)
except IOError:
print 'Error: Unable to write LaunchAgent'
success = False
try:
os.chmod(path, 0644)
os.chown(path, 0, 0)
except:
print 'Error: Unable to set permissions and ownership on LaunchAgent'
success = False
return success
def main():
'''Main'''
job = {
'Label': 'com.trusourcelabs.NoMAD',
'KeepAlive': True,
'LimitLoadToSessionType': ['Aqua'],
'ProgramArguments': ['/Applications/NoMAD.app/Contents/MacOS/NoMAD'],
'RunAtLoad': True
}
if add_launchagent(job):
print 'Success: LaunchAgent added'
sys.exit(0)
else:
print 'Error: Unable to add LaunchAgent'
sys.exit(1)
if __name__ == '__main__':
main()
#!/usr/bin/python
'''
Load NoMAD LaunchAgent
Uses launchctl 'asuser' command, so 10.10+ only (just like NoMAD)
'''
import os
import sys
import subprocess
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
def getconsoleuser():
'''Uses Apple's SystemConfiguration framework to get the current
console user'''
cfuser = SCDynamicStoreCopyConsoleUser(None, None, None)
return cfuser
def main():
'''Main'''
la = '/Library/LaunchAgents/com.trusourcelabs.NoMAD.plist'
if os.path.exists(la):
username, uid, gid = getconsoleuser()
cmd = ['/bin/launchctl', 'asuser', str(uid), '/bin/launchctl', 'load',
la]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
proc.communicate()
if proc.returncode == 0:
print 'Success: Loaded LaunchAgent {}'.format(la)
sys.exit(0)
else:
print 'Error: Unable to load LaunchAgent {}'.format(la)
sys.exit(1)
else:
print 'Error: LaunchAgent not found at {}'.format(la)
sys.exit(1)
if __name__ == '__main__':
main()
#!/usr/bin/python
'''
NoMAD Pre-Update
'''
import os
import sys
import subprocess
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
def getconsoleuser():
'''Uses Apple's SystemConfiguration framework to get the current
console user'''
cfuser = SCDynamicStoreCopyConsoleUser(None, None, None)
return cfuser
def unload_launchagent(label, userid):
'''Unloads a LaunchAgent by label'''
status = True
la = '/Library/LaunchAgents/{}.plist'.format(label)
if os.path.exists(la):
cmd = ['/bin/launchctl', 'asuser', str(userid), '/bin/launchctl',
'unload', la]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
proc.communicate()
if not proc.returncode == 0:
status = False
return status
def kill_process(process):
'''Kills a process by name'''
status = True
cmd = ['/usr/bin/pgrep', process]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, _ = proc.communicate()
if out:
cmd = ['/bin/kill', '-9', str(int(out))]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
proc.communicate()
if not proc.returncode == 0:
status = False
return status
def main():
'''Main'''
username, uid, gid = getconsoleuser()
if username:
if unload_launchagent('com.trusourcelabs.NoMAD', uid):
print 'Success: Unloaded LaunchAgent'
else:
print 'Error: Unable to unload LaunchAgent'
if kill_process('NoMAD'):
print 'Success: Killed NoMAD'
else:
print 'Error: Unable to quit NoMAD'
sys.exit(0)
else:
print 'Info: No user logged in; nothing to unload or quit'
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment