Applescript for running sudo commands in terminal

I'm a newbie when it comes to Applescript and was wondering if someone could help with a basic request. I need to write a small script to do the following:
1. Launch terminal
2. Run the 'Sudo -s' command
3. Enter the administrator password (in plain text)
and then
4. run some sudo commands like "sudo defaults write /Library/Preferences/com.apple.loginwindow HiddenUsersList -array-add administrator"
I actually WANT the administrator password to be in the script in plain text even though I understand the security risks (I will literally be the only person to ever see the script). I've trawled forums all over but can't seem to find what I am looking for and cannot get it working by patching together the various commands I have found. Thanks in advance for any help you can give!
Sean

I've included two examples.  The preferred way & the hacker way.
with administrator
It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
    Author: rccharles
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
    For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set unixDesktopPath to POSIX path of "/System/Library/User Template/"
    log "unixDesktopPath = " & unixDesktopPath
    set quotedUnixDesktopPath to quoted form of unixDesktopPath
    log "quoted form is " & quotedUnixDesktopPath
    try
        set fromUnix to do shell script "sudo ls -l  " & quotedUnixDesktopPath with administrator privileges
        display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
    on error errMsg
        log "ls -l error..." & errMsg
    end try
end run
This version has an inline password.
Notice the echo 'password' |
The single quotes are no accident.
It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
    Author: rccharles
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
    For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set unixDesktopPath to POSIX path of "/System/Library/User Template/"
    log "unixDesktopPath = " & unixDesktopPath
    set quotedUnixDesktopPath to quoted form of unixDesktopPath
    log "quoted form is " & quotedUnixDesktopPath
    try
        set fromUnix to do shell script "echo 'password' | sudo ls -l  " & quotedUnixDesktopPath
        display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
    on error errMsg
        log "ls -l error..." & errMsg
    end try
end run

Similar Messages

  • How to create script that run sudo-command?

    I often need to run command:
    sudo "/Library/Application Support/VMware Fusion/boot.sh" --restart
    This needs to be run as admin.
    Can anybody tell me how to create script that will login as admin and run that command in terminal?
    Or from where can I get help how to add commands to a script?
    Thanks
    Tomi

    Your best bet on getting a cogent answer is to post to the Unix forum under OS X Technologies.

  • Problem running sudo command in script

    Hello,
    I try to run this script.
    #!/bin/bash
    sudo /bin/cat /dev/input/event1 >> /home/tom/.log/log.bin &
    The sudoers entry
    %users ALL= NOPASSWD: /bin/cat /dev/input/event1 >> /home/tom/.log/log.bin
    I get the following error!
    $ Passwort: sudo: pam_authenticate: Kommunikationsfehler
    Kommunikationsfehler = eng: communication error
    What's wrong with that?
    Thank you!
    clementis

    Sorry, for being not precise.
    I created the alias in .bashrc in my $home directory. In sudoers all users are allowed to key in this command without a passwort entry.
    I know how to autostart in xfce and so created an autostart entry but unfortunatly it doesn't work for my "special" command.
    What I did:
    1. I wrote a script (first post) and tried to autostart it in xfce. For that command I created an entry in sudoers in order to avoid enter root passwort. The script didn't start.
    Then...
    2. I created an autostart entry (deleted the first one - see point 1) and entered the whole command (cat .../event1 >> $home...) in the xfce autostart input window. The idea was to run the command directly and not through a script. But it didn't work.
    Therefore I tried to run alter the command to "bash cat .../event1 >> $home ...." Didn't work either.
    3. I also deleted the autostart entry no 2. and created the alias in .bashrc in my home directory. Now created a new autostart entry trying to start the alias. Didn't work.
    I also tried to set the Terminal option in the file $home/.config/autostart/log.desktop to "true".
    So it looked like this:
    [Desktop Entry]
    Encoding=UTF-8
    Version=0.9.4
    Type=Application
    Name=log
    Comment=event1
    Exec= sudo cat /dev/input/event1 >> /home/tom/.cblog/log.bin &
    StartupNotify=false
    Terminal=true
    Hidden=false
    The alias for example works well if I enter it in xterm.
    So, I don't know why it isn't working! :-(
    Thank you for any help
    Clementis

  • How to create script that run sudo-command, via automator?

    Hi
    I'd want to create script to automate one command I need quite often. I just can't get this to work.
    I'm not using english OS, but I think this is what I do. In automator I choose Utilities -> Run script
    Here's the script I try to run:
    sudo "/Library/Application Support/VMware Fusion/boot.sh --restart"
    I found some tip to do it like this:
    do shell script "sudo /Library/Application Support/VMware Fusion/boot.sh --restart password "pass" with administrator privileges
    Where pass is my admin password.
    This does not work, either.
    Could anybody help me on this?
    Thanks...
    Tomi Toivonen
    Message was edited by: Tomi

    This is not working. What's wrong?
    What's wrong is that the '--restart' is a parameter for boot.sh, and therefore needs to be included within the quotes.
    Additionally, the shell uses spaces to divide parameters, so the spaces in the command will make it appear as multiple commands - '/Library/Application', 'Support/VMware' and 'Fusion/boot.sh'. You need to escape them using a backslash:
    <pre class=command>do shell script "/Library/Application\ Support/VMware\ Fusion/boot.sh --restart" password "pass" with administrator privileges</pre>
    If it's outside of the quotes it would be interpreted as a parameter to 'do shell script' and it won't know what to do with that.

  • [SOLVED] Running sudo command to be executed on dmenu

    I'm using "sudo grub-reboot 2". What is the best way to run this command with dmenu, considering that sudo is necessary, without sacrificing security?
    Last edited by Quatro (2014-08-19 00:22:24)

    My dmenu power menu script looks like this.
    power_menu.sh
    #!/bin/bash
    # dmenu2 power menu
    OPT=$(echo -e "suspend\npoweroff\nreboot" | dmenu -f -h 28 -fn "Ubuntu Mono-12" -x 460 -y 370 -w 360 -nb black -nf white -sb white -sf black )
    case $OPT in
    suspend) sudo systemctl suspend ;;
    poweroff) sudo systemctl poweroff ;;
    reboot) sudo systemctl reboot ;;
    esac
    And the following file allows me to run those commands without being prompted for a password.
    /etc/sudoers.d/include
    slithery ALL=(ALL) NOPASSWD:/usr/bin/systemctl suspend
    slithery ALL=(ALL) NOPASSWD:/usr/bin/systemctl poweroff
    slithery ALL=(ALL) NOPASSWD:/usr/bin/systemctl reboot
    Just use these as an example and you should get what you're after.
    Last edited by slithery (2014-08-18 11:52:31)

  • Applescript to run ant command

    Hi,
    I need an applescript to run an ant command "ant init clean compile run".
    Please help.
    Thanks.

    Here is a very abbreviated AppleScript. It launches a terminal window and runs ant in it. Using the AppleScript editor in /Applications/Utilities, you can compile this and save as an application. I called mine antics. This will leave a spare Terminal window laying about, which you may find useful to review any ant output. If no Java is installed, the ant activity will trigger a system dialog requesting Java installation.
    --AppleScript
    tell application "Terminal"
      activate
              do script "/usr/bin/ant init clean compile run"
    end tell

  • How to disable sudo command in Terminal

    How to disable sudo command in Terminal

    Bad idea.
    Only an Admin user can use sudo, so if you have users on your system that you do not want to use sudo, then do not give them an admin account, and do not give them the password for an admin account.

  • After typing a sudo command on terminal how do you save it so you dont have to keep giving that same command every time I restart my mac??

    I have a 45mb/s internet at home and my youtube videos load extremely slow, after reading many blogs and empting my cache cleaning my history I stumble across a post that told me i had to block two IP's on the terminal using this two commands: " sudo ipfw add reject src-ip 173.194.55.0/24 in" and "sudo ipfw add reject src-ip 206.111.0.0/16 in" that it should help. After I did that and restarted the mac those commads are undone. Is there a way to save thos commands tipped in the terminal??

    Open the AppleScript Editor in the /Applications/Utilities/ folder and paste in the following:
    set thepass to text returned of (display dialog "Your administrator password is needed." default answer "" buttons {"Cancel", "OK"} default button "OK")
    do shell script "ipfw add reject src-ip 173.194.55.0/24 in" password thepass with administrator privileges
    do shell script "ipfw add reject src-ip 206.111.0.0/16 in" password thepass with administrator privileges
    Save it as an application and add it to the list of login items for your user account in the Users & Groups pane of System Preferences.
    (81466)

  • Applescript for running specific action from action wizard...

    Hi Forum,
    using this applescript I m running my "INDEGN" action from action wizard...
    the first step is activated through the applescript.. (ie. INDEGN action is correctly picked)
    but how to "click next button" to proceed to next steps..
    tell application "System Events"
        tell application process "Acrobat"
            tell application "Adobe Acrobat Pro" to activate
            click the menu item "INDEGN" of menu 1 of menu item "Action Wizard" of the menu "File" of menu bar 1  ## here it is not cliking next button.....
            click button "Next" of window "Action: INDEGN"
    click button "Close" of window "Action: INDEGN"
        end tell
    end tell
    many thanks for the support..

    HI DAVE,
    thanks for the reply,,
    Is there any other way to run "my action" from action wizard. using script....
    Would be  a great help if advised....
    Many thanks...

  • Root Password Doesn't Work on Sudo Commands

    I went through the process of changing the root password in Recovery mode and it appeared everything went as expected. Rebooted the Mac. When I run a command in Terminal using "sudo", I get challenged for the root password, and authentication fails. Are there any reasons that might explain this, or is there something I need to do differently? The laptop is supplied by my work, and I have both an admin and a standard account on it. When I'm prompted for admin authentication, entering the admin username and password works fine. But I need to be able to do things that require sudo permission (like reload the hosts file without having to do a full reboot), and it's just not responding to the password I just reset. Thanks for any help.

    Does your work know you enabled the root password on their hardware? They may consider that a security risk it isn't normal for Mac users to do this and it defeats the point of having sudo installed too.
    Read the manual…
    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/ man8/sudo.8.html
    man sudo
    (NOTE: in the default configuration this is the user's password, not the root password).
    You need to be an admin user to use sudo, standard accounts do not have access to it.

  • PLEASE HELP making a program that uses sudo commands

    hey
    is there anyway to make a program run sudo commands
    i have a problem because in a terminal it would ask you to input a password.
    is there a way to use a fake keyboard program to input the password in the back ground
    of my java application or some how run sudo commands? my application relies on some outputs of these commands.
    please help thanks.

    ill tell you a bit about my program im making.
    i have a wireless usb that i have to switch back and forth to get my paticullar drivers to
    do certain things e.g. one is for web browsing and the other one has packet injection.
    by typing sudo modprobe ect.. i can switch through a terminal. which requires me to type in a password.
    iv made a application which uses the
    Runtime.getRuntime().exec("sudo ...."); it has 2 buttons to switch from diffrent drivers and always runs at start up.
    just a big problem i cant use sudo it just freezes when i start and click on the buttons. and doesnt ask me for a password.
    can anyone help thanks

  • Sudo Command with installation

    Hi,
    I am trying to install a software called Fullprof suite. Upon downloading, I have to follow the directions listed here:
    http://www.ill.eu/sites/fullprof/php/downloads.html
    I'm supposed to use the sudo command in terminal, which may lead to data loss. I'm pretty new to this sort of thing, so I would like to confirm that the text I am entering is safe, and only allows installation of the program:
    sudo ln -s /Applications/Darwin/openmotif-2.2.3-universal /usr/OpenMotif
    I am running on MacBook (2008), 10.5.8.
    Any help would be appreciated. I tried running this program on our pc and now that computer doesn't start....

    1. Revokes the permissions the first command granted so that nothing else can take advantage of them.
    2. Because it's making changes to system-level folders. This is the same reason why most of Apple's software updates require an administrator password.
    (77525)

  • I want to disable 'Spotlight' for repairing my disk "Macintosh HD" , but somehow the command 'sudo mdutil -a -i off' does not work in 'Terminal'. I get result as ' bash: sudo: command not found' .I am using OS 10.10.  Please help.

    I want to disable 'Spotlight' for repairing my disk "Macintosh HD" , but somehow the command 'sudo mdutil -a -i off' does not work in 'Terminal'. I get result as ' bash: sudo: command not found' .I am using OS 10.10.  Please help.

    It appears that you may not have a properly installed OS X system.
    You must be connected to the Internet to reinstall OS X.
    In the menu bar, choose Apple menu > Restart. Once your Mac restarts (and the gray screen appears), hold down the Command (⌘) and R keys.
    Select “Disk Utility,” and then click Continue.
    Select your startup disk from the list on the left, and then Repair Disk.
    After disk is repaired, select Reinstall OS X, then click Continue.
    Follow the onscreen instructions. In the pane where you select a disk, select your current OS X disk

  • Run a sudo command on system startup via Applescript?

    Hey, I'm trying to hide the Spotlight icon on my menu bar using the sudo command shown here:
    http://osxdaily.com/2011/12/12/hide-spotlight-menu-icon-mac-os-x-lion/
    It works fine, but when I reboot my Mac (I tend to turn it off at night since it has a habit of waking from sleep by itself) the icon has re-appeared, and I need to run the command again.
    I'd ideally like to write an Applescript to run on launch to execute the two commands (which also requires entering my password), but I've not been successful, I get various errors talking about authentication and such.
    Is there an easier way to get this icon permanently hidden? I use Alfred, so while I make use of the Spotlight index, I never touch the magnifying glass icon, and I like to keep a clean menu bar!

    You don't need applescript. What you need to create is a LaunchAgent. Lingon (available through the App Store) can help you write it. But, there are other sources that can show you how to write the raw file and put it in the LaunchAgents folder, like here: http://www.devdaily.com/mac-os-x/launchd-examples-launchd-plist-file-examples-ma c

  • Can't run bash or sudo commands

    I have a Mac Pro with OS X 10.8.2 and also an older iMac running OS X 10.6.8. On neither Mac can I run any bash or sudo commands! I always get command not found.
    Both Macs have fresh installs of OS X, did I forget to install something? Is X11 needed? or Xcode?
    Thanks for any help.

    Back up all data.
    Select
    Shell ▹ New Command
    from the Terminal menu bar. Copy and paste the following line into the text box that opens:
    mkdir disabled_shell_files; mv .profile .bash* $_
    Your old shell initialization and history files will be saved in a directory with the indicated name at the top level of your home directory.

Maybe you are looking for