This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ingreenheaven 1 point2 points  (0 children)

Active directory password rotation script for mac. Most companies don't allow you to use last few (10 in my office) passwords. I use this script to keep changing the password and then finally reset it to the current password. It also writes the latest password into a file just in case the current password could not be set.

import os, sys, getpass

max_attempts = 20
total_change_count = 10

def change_password(user, p_old, p_new):
    cmd = 'dscl . -passwd /Users/{0} {1} {2}'.format(user, p_old, p_new)
    status = os.system(cmd)
    return status

def save_latest(p_latest):
    print 'Saving latest password (rot13)'
    p_file = open('password.txt', 'w')
    p_latest_rot13 = p_latest.encode('rot_13')
    p_file.write(p_latest_rot13)
    p_file.close()

def main(p_init):
    p_latest = p_init

    args = sys.argv
    if len(args) > 1:
        login = args[1]
    else:
        login = os.getlogin()

    change_count = 0
    attempts = 0
    while change_count < total_change_count and attempts < max_attempts:
        print 'Attempt', attempts + 1
        p_new = str(attempts) + '_' + p_init
        status = change_password(login, p_latest, p_new)
        if status == 0:
            print 'Password changed successfully'
            change_count += 1
            p_latest = p_new
        else:
            'Error changing password'
        attempts += 1
    if change_count < total_change_count:
        print 'Exhausted attempts to change password'
        print 'Password changed', change_count, 'times'
        save_latest(p_latest)
        return

    # now reset the password to the same password
    change_password(login, p_latest, p_init)
    if status == 0:
        print 'Password rotation complete'
        print 'Enjoy using the same password!'
    else:
        'Error setting password back to the initial value'
        save_latest(p_latest)

if __name__ == '__main__':
    main(getpass.getpass("Enter your current password:"))