Error when running a script /usr/bin/python: bad interpreter: No such file or directory

bryan_garrant
New Contributor III

Hi, I am trying to run a script from Duo to restore settings after 12.3 upgrade.

 It runs fine locally with no error  but if I try though JAMF I get the error "/usr/bin/python: bad interpreter: No such file or directory" 

Any idea on what might be causing this?

The script is here:

#!/usr/bin/python

import os
import plistlib
import subprocess
import sys
import tempfile

from subprocess import PIPE


## Mechs that support MacLogonPlugin
maclogon_mechs = ["MacLogon:Check,privileged","MacLogon:DuoAuthGUI"]
maclogon_index_mech = "loginwindow:done"
maclogon_index_offset = 0

def bash_command(script, getoutput=True):
    try:
        if getoutput:
            return subprocess.check_output(script)
        else:
            return subprocess.call(script, stderr=PIPE)
    except (subprocess.CalledProcessError, OSError) as err:
        sys.exit("[* Error] **%s** [%s]" % (err, str(script)))

def remove_mechs_in_db(db, mech_list):
    for mech in mech_list:
        for old_mech in filter(lambda x: mech in x, db['mechanisms']):
            db['mechanisms'].remove(old_mech)
    return db

def set_mechs_in_db(db, mech_list, index_mech, index_offset):
    ## Clear away any previous configs
    db = remove_mechs_in_db(db, mech_list)

    ## Add mech_list to db
    i = int(db['mechanisms'].index(index_mech)) + index_offset
    for mech in mech_list:
        db['mechanisms'].insert(i, mech)
        i += 1
    return db

def edit_authdb():
    ## Create a temporary file used to write/read plist information
    with tempfile.NamedTemporaryFile(delete=False) as temp_plist_file:
        ## Export "system.login.console" and read it into the temp file
        system_login_console = bash_command([
            "/usr/bin/security",
            "authorizationdb",
            "read",
            "system.login.console"])
        temp_plist_file.write(system_login_console)
        temp_plist_file.close()

        ## Leave the for loop.
        for p in [temp_plist_file.name]:
            ## Parse the plist
            d = plistlib.readPlist(p)
            ## Add MacLogon mechs
            d = set_mechs_in_db(d, maclogon_mechs,
                                maclogon_index_mech, maclogon_index_offset)
            ## Write out the changes
            plistlib.writePlist(d, p)

        ## Read the edited plist file back into the authorizationdb command
        with open(temp_plist_file.name, "r") as temp_plist_file:
            p = subprocess.Popen([
                "/usr/bin/security",
                "authorizationdb",
                "write",
                "system.login.console"],
                stdout=PIPE, stdin=PIPE, stderr=PIPE)
            stdout_data = p.communicate(input=temp_plist_file.read().encode())
            temp_plist_file.close()

def check_root():
    if os.geteuid() != 0:
        sys.exit("Only root can run this script.")

def check_plugin_exists():
    plugin_path = "/Library/Security/SecurityAgentPlugins/MacLogon.bundle"
    if not os.path.exists(plugin_path):
        sys.exit("MacLogon bundle not found. Please reinstall MacLogon.")

def check_prefs_exists():
    prefs_path = "/private/var/root/Library/Preferences/com.duosecurity.maclogon.plist"
    if not os.path.exists(prefs_path):
        sys.exit("MacLogon preferences plist not found. Please reinstall MacLogon.")


def main(argv):

    check_root()
    check_plugin_exists()
    check_prefs_exists()    
    edit_authdb()


if __name__ == '__main__':
    main(sys.argv)
1 REPLY 1

sdagley
Esteemed Contributor II

@bryan_garrant Python 2 was removed in macOS Monterey 12.3. If you still need it then you'll have to install it yourself, but it can't be installed in /usr/bin/python. See the SUS Inepector 2.1 releases page for a couple of different options for installing Python 2: https://github.com/hjuutilainen/sus-inspector/releases/tag/v2.1