|
#!/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() |