Sudo under AppleScript ???

Discussion in 'General Mac Discussion' started by wifi_cowboy, May 8, 2004.

  1. macrumors newbie

    Joined:
    Nov 15, 2003
    Location:
    Dark Side of the Moon
    #1
    Hey guys,
    I am having a hell of a time trying to Sudo with an AppleScript.
    Heres the code...

    do shell script "sudo ifconfig en0 down; sudo ifconfig en0 up"

    When executed, it says AppleScript Error password: password: . Any ideas how this can be done to automate the password? I have tried everything I know, and to no avail. Oh, thanks for your help in advance ;) .
     
  2. macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #2
    Here's how!

    METHOD #1, not so secure if many people are going to read the AppleScript source, but tons easier to do:

    Read what cello wrote below.

    METHOD #2, a little bit more secure:

    If you're not worried about ANYONE doing sudo ipconfig's without a password (it has to be your username anyway so not much of a concern there) on your system, you can have sudo automatically run it without a password. Here's how!

    Code:
    $ sudo visudo
    password:
    You'll be presented with the /etc/sudoers file in vi.
    Code:
    # sudoers file.
    #
    # This file MUST be edited with the 'visudo' command as root.
    #
    # See the sudoers man page for the details on how to write a sudoers file.
    #
    
    # Host alias specification
    
    # User alias specification
    
    # Cmnd alias specification
    
    # Defaults specification
    
    # User privilege specification
    root    ALL=(ALL) ALL
    %admin  ALL=(ALL) ALL
    Type "i" without the quotes. Hit the down arrow key until you get to the bottom and the right arrow key until you get to the end of that line, then hit enter in order to add the line:
    Code:
    [I]username_under_which_you're_running_the_script[/I] ALL=NOPASSWD:/sbin/ifconfig
    This should be like on my machine:
    Code:
    will ALL=NOPASSWD:/sbin/ifconfig
    Hit the escape key.
    Type ":w" to save the file.
    Type ":q" to quit.

    You should be good. You can e-mail me if you've any problems.

    Thanks to cello for some vi tutorial, as I had forgotten much of vi.
     
  3. macrumors newbie

    Joined:
    Feb 29, 2004
    Location:
    in front of my computer
    #3
    do it like this:
    do shell script "sudo ifconfig en0 down; sudo ifconfig en0 up" password "your_password" with administrator privileges

    hope this helps...
     
  4. macrumors newbie

    Joined:
    Jun 7, 2004
    #4
    Keeping passwords out of Applescript code.

    You can use Applescript property assignments to save sensitive data such as passwords into the Script. Because Applescript is not in plain text, it is very difficult for someone to open the file and read the password. To do the property assignment we do:

    Code:
    property userpassword : ""
    
    if userpassword is "" then
       display dialog "Please enter your password:" default answer ""
       set userpassword to text returned of result
    -- The repeat section below is an optional error checking routine to ensure the password is valid
    set the_password to "Undefined"
       repeat until the_password is "Correct"
          try
             set theFinderPID to do shell script "ps -axww | /usr/bin/grep '[/]Finder'| awk '{print $1}' | head -1"
             do shell script "renice 1 " & theFinderPID password userpassword with administrator privileges
             do shell script "renice 0 " & theFinderPID password userpassword with administrator privileges
    	   set the_password to "Correct"
    	on error
             display dialog "Sorry, the password entered was not correct. Please try again:" default answer ""
             set userpassword to text returned of result
         end try
       end repeat
    end if
    
    do shell script "sudo ifconfig en0 down; sudo ifconfig en0 up" password userpassword with administrator privileges
    
    However, this password information will not be saved if the script is opened and edited again in Script Editor, or if the script doesn't exit properly.

    More information is available at: Macscripter FAQ
     

Share This Page