Change account password via AppleScript

Discussion in 'macOS' started by Cor anglais 16, Feb 6, 2011.

  1. Cor anglais 16 macrumors newbie

    Joined:
    Jan 7, 2010
    #1
    I don't have an account password set on my machine, and I sometimes use sudo -s in the Terminal to do advanced stuff. Since 10.6, Apple has required an account password to use sudo, which requires me to open the Accounts preference pane and reset my account password to a dummy password (I just use the number 1) to enable the sudo shell. Then I have to trek back there to reset it to nothing after my sudo business is finished. I was looking to make an AppleScript that would enable me just to double-click an app-ified AppleScript to change the password to "1" and back to nothing again. So far, I've found this script:

    Code:
    do shell script "dscl . -passwd /Users/[username] 1"
    
    That's great, but it doesn't work the other way around:

    Code:
    do shell script "dscl . -passwd /Users/[username]  "
    
    gives me an error: "Please enter the user's old password."

    My original idea was to use this script as a starting point, but I'm not well-versed enough in AppleScript to adapt it successfully:

    Code:
    tell application "System Preferences"
      activate
    end tell
    
    tell application "System Events"
      tell process "System Preferences"
        click menu item "Sharing" of menu "View" of menu bar 1
        delay 2
        tell window "Sharing"
          click checkbox 1 of row 11 of table 1 of scroll area 1 of group 1
          delay 1
          if (exists sheet 1) then
            if (exists button "Turn AirPort On" of sheet 1) then
              click button "Turn AirPort On" of sheet 1
              delay 1
            end if
            click button "Start" of sheet 1
          end if
        end tell
      end tell
    end tell
    Does anyone have any ideas that would help me out? Thanks!
     
  2. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #2
    What a ridiculous solution.

    Also, this started in 10.5

    To get to the issue. That script you posted is worthless, it has a bunch of worthless steps of clicking through menus to get a simple task done.

    In regards to the dscl commands, the second one doesn't work because you need administrator privileges to change the password, in other words you have to enter a password to change it at some point. The first one works since you have no password at that point. Change from 1 to 2 and you will get the same error.

    So using:

    Code:
    do shell script "dscl . -passwd /Users/username ''" with administrator privileges
    Which would then require you to enter 1 as the password would work (you could also save the password in there, but that would require you to write some password detection mechanisms to determine if it is blank or not, or write it with buttons so you can select).

    Solution:
    But as I said, this is a silly solution. If you really want to role this loose while you do "advanced" stuff. Then simply modify sudoers to require no password for any command, this will get around the non-blank password requirement.

    Another Solution:
    You essentially want to be root, which is what su can do. Enable the root user, type su into terminal, enter the password you used and from there you won't have to put sudo before anything. Just fire off the commands.

    Another Solution:
    Login as root and use that as your primary account.
     
  3. Cor anglais 16 thread starter macrumors newbie

    Joined:
    Jan 7, 2010
    #3
    Thanks… I had not considered any of your alternative (better) solutions, such as living life from the root account. I'll consider using su instead of sudo as well. But this seems like the best solution to meet the needs of my workflow at the moment:

    Very cool indeed (if a bit dangerous, though in my case there is a very low likelihood of my iMac being hijacked by someone who would know how to exploit a passwordless sudo)… how might I go about modifying sudoers?
     
  4. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #4
    Code:
    sudo visudo
    Add this line replacing shortname with your shortname, in the User privilege specification section:

    Code:
    shortname   ALL=(ALL) NOPASSWD: ALL
    If you haven't used vi before you may want to research how to use it. The basics would be:

    Press 'i' in order to modify.
    When done, hit escape, then type ':wq'

    Be careful, you can really screw this up and lose sudo altogether. Make a backup of the original first. (it is in /etc)
     
  5. Cor anglais 16 thread starter macrumors newbie

    Joined:
    Jan 7, 2010
    #5
    I have modified sudoers as described, and everything seems to be working properly. I have logged out and back in to my user account and nothing is awry (though I haven't restarted the computer yet). I'll hang onto the backup of sudoers in case something crazy manifests itself in the future. This seems to have done the trick; thanks a lot!
     

Share This Page