Reputation: 744
Hello,
In my AppleScript I am running the following code,
do shell script "sudo networksetup -setproxybypassdomains Ethernet *.local, 169.254/16"
do shell script "sudo networksetup -setwebproxy Ethernet 127.0.0.1 8888"
do shell script "sudo networksetup -setsecurewebproxy Ethernet 127.0.0.1 8888"
However, it asks to enter password every time. I mean when the script is opened it asks for password 3 times for each line of process. In order to prevent from asking password I have to type the following in the Terminal,
sudo chmod u+s /usr/sbin/networksetup
When the Enter key is pressed it asks for the password and once entered and run the AppleScript then it doesn't ask for password (3 times) anymore.
But the user has to go to Terminal and type the above command. In order to prevent this I used the following code in the AppleScript,
do shell script "sudo chmod u+s /usr/sbin/networksetup"
So the user runs the AppleScript and it will auto set. But I get the following error message,
How to change the AppleScript for,
do shell script "sudo chmod u+s /usr/sbin/networksetup"
...to make it ask for password once and then execute the rest of the "...do shell script..." parts without asking for passwords. Or add the password along with the above line of code to make it work?
Upvotes: 1
Views: 1563
Reputation: 39
New answer to old question, but since no answer included both retention of the name & password plus proper hiding of the password during entry, this may help someone drawn to the question. This does both, and then uses name & password in a shell script similar(enough) to the OP's. The name & password are stored as properties of the script, but remain invisible. They are automatically "erased" if the script changes, and will need to be reentered for only the first run. If name or password are incorrect, then the script exits gracefully before continuing to any following code. "sudo" is not needed in a shell script, as long as it is run "with administrator privileges". Normally, just running "with administrator privileges" will force a pop-up to ask for name & password. But since they are already included in this example, the script silently applies the stored name & password, and no pop-up occurs after the first run.
----------ENTER NAME & PASSWORD ONCE*----------
property admin : null --resets to null after each edit*
property pswrd : null --resets to null after each edit*
#--#--#--NOT A SECURE WAY TO STORE PASSWORDS!--#--#--#
repeat
try
if admin is null then set admin to (text returned of (display dialog "Enter username:" default answer "")) --get name.
if pswrd is null then set pswrd to (text returned of (display dialog "Enter password for " & admin & ":" default answer "" with hidden answer)) --get pw.
do shell script "whoami" user name admin password pswrd with administrator privileges --test name & pw.
exit repeat --name & pw were OK!
on error err
set {admin, pswrd} to {null, null}
if err is "User canceled." then return err
display dialog err with icon 0
end try
end repeat
--beyond this line, admin-name & password are retained in the admin & pswrd properties!
----------EXAMPLE OF NAME & PASSWORD USEAGE----------
do shell script "networksetup -listallhardwareports" user name admin password pswrd with administrator privileges
Upvotes: 0
Reputation: 19032
Have you looked at the definition for the "do shell script" command? You don't use sudo with that command. You would use "with administrator privileges" and you can supply a "user name" and "password" if you wish.
Do this... open AppleScript Editor. Under the File menu choose "open dictionary". Select StandardAdditions.osax from the dialog window. In the search field of the dictionary search for "do shell script" to see everything that you can do with that command.
For example, here's how you could use it. Good luck.
do shell script "networksetup -setproxybypassdomains Ethernet *.local, 169.254/16" with administrator privileges
Upvotes: 5
Reputation: 4056
You can pass the password as a parameter to the sudo
. From the man page:
-S The -S (stdin) option causes sudo to read the password from
the standard input instead of the terminal device. The
password must be followed by a newline character.
Upvotes: 0