PDA

View Full Version : Sudo under AppleScript ???


wifi_cowboy
May 8, 2004, 08:25 PM
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 ;) .

MacCoaster
May 9, 2004, 03:00 PM
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!

$ sudo visudo
password:
You'll be presented with the /etc/sudoers file in vi.
# 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:
username_under_which_you're_running_the_script ALL=NOPASSWD:/sbin/ifconfig
This should be like on my machine:
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.

cello
May 9, 2004, 03:06 PM
do it like this:
do shell script "sudo ifconfig en0 down; sudo ifconfig en0 up" password "your_password" with administrator privileges

hope this helps...

Brontojoris
Jun 7, 2004, 11:33 PM
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:


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 (http://macscripter.net/faq/comments.php?id=191_0_10_0_C)