Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have this script lines from my ISP:

sudo bash
echo "plugin L2TP.ppp">>/etc/ppp/options 
echo "l2tpnoipsec">>/etc/ppp/options

It works if I paste line by line into Terminal. I want to create a *.command file and run it by double-clicking. But all I get is password prompting and then empty bash window. The resulting "options" file is empty.

I tried this:

#!/bin/bash

echo "plugin L2TP.ppp">>/etc/ppp/options 
echo "l2tpnoipsec">>/etc/ppp/options

I get:

/etc/ppp/options: Permission denied

I think I need to use some command to get root privileges from inside bash.

share|improve this question
    
When you created your script did you execute it by typing sudo ./ispscript.command? Also, bash scripts usually have a .sh suffix: ipscript.sh. –  Aaron Lake Apr 26 '12 at 14:11
1  
FYI This script should only need to be run once. Also, if you keep running the script as is, it's APPENDING >> to the file. The options file is going to continue to grow in size the more you run it(the script). This may or may not cause issues with what ever is reading the options file. –  dennis.hempler Apr 26 '12 at 16:19
    
@dennis.hempler, thanks I know this. Actually I will run this script often. Because USB-modem device needs this file to be blank to operate properly. So before inserting USB-modem I erase the file and before connecting to wired internet I write those options in the file. –  geotavros Apr 26 '12 at 20:19

4 Answers 4

up vote 10 down vote accepted

Take the script that you created:

#!/bin/bash

echo "plugin L2TP.ppp">>/etc/ppp/options 
echo "l2tpnoipsec">>/etc/ppp/options

Save it in your home directory, or a 'scripts' directory inside your home directory, as l2tp.sh. Allow it to be executed(write this command in Terminal):

chmod 700 ~/path/to/l2tp.sh

To execute the file using sudo (root privileges):

Method #1. In Terminal type:

$ sudo ~/path/to/l2tp.sh

Method #2. Create a file run_l2tp.command with this contents:

sudo ~/path/to/l2tp.sh

Allow it to be executed:

chmod u+x run_l2tp.command

When you double-click run_l2tp.command and enter the password the l2tp.sh file will be executed with root privileges.

Method #3 If for security purposes, you don't want any user of your system to be able to run the script, but rather you want to be prompted for an administrative password, an alternate solution would be to save the shell script and then use the program AppleScript Editor to create an AppleScript.

The AppleScript would be a one-liner, saying do shell script «your script's name here» with administrator privileges. Save that script as an Application. Then, when you click it, it will ask you for an administrator password, then run the shell script with administrator privileges.

Obviously, replace «your script's name here» with the path to your script.

Some notes:

  • On UNIX like systems, ~ is short for "my home directory".
  • Chmod 700 will make the file executable only by you. For more information: see this Wikipedia page.
  • typing 'sudo' before a command will execute the program using root privileges. Be careful when doing this, bad things can happen if you're not sure what you're doing.
  • Obviously you can omit the /path/to if you saved this script directly in your home directory.
share|improve this answer
    
Why did you revert my edits? In my question I was asking about double-clicking without running terminal and manually typing. And edited your answer appropriately. –  geotavros Apr 26 '12 at 17:30
    
Reapplied your changes. –  Aaron Lake Apr 26 '12 at 17:34

Save this:

#!/bin/bash

echo "plugin L2TP.ppp">>/etc/ppp/options 
echo "l2tpnoipsec">>/etc/ppp/options

to your Desktop in a file named script.sh.

Open a Terminal window and type:

sudo bash ~/Desktop/script.sh

Enter your password when prompted and all the commands in the file will run with super user privledges.

share|improve this answer
    
passing the script (with whatever permissions it may have) to an executable with the correct permissions is elegant. –  bmike Apr 26 '12 at 15:11
    
Thanks for your efforts, upvoted –  geotavros Apr 26 '12 at 19:55
    
@geotavros I missed that you wanted it click-able only. Opps. Appreciate the vote though. –  Ian C. Apr 26 '12 at 19:58

If for security purposes, you don't want any user of your system to be able to run the script, but rather you want to be prompted for an administrative password, an alternate solution would be to save the shell script and then use the program AppleScript Editor to create an AppleScript.

The AppleScript would be a one-liner, saying do shell script «your script's name here» with administrator privileges. Save that script as an Application. Then, when you click it, it will ask you for an administrator password, then run the shell script with administrator privileges.

Obviously, replace «your script's name here» with the path to your script.

share|improve this answer
1  
This is identical to the sudo method. With sudo you're required to have an Admin level account. What you're suggesting takes a script and wraps it in a script. –  Aaron Lake Apr 26 '12 at 17:16
    
No, it's not identical. This takes a Terminal script and wraps it in a GUI script -- so the user never has to wade into terminal, and is prompted for a password in the familiar Mac GUI and not in a Terminal window. For a technical user, this may appear to be the same thing; for most end users, not having to use Terminal makes a world of difference. –  Daniel Lawson Apr 26 '12 at 17:36
1  
You referenced "for security purposes", if it was for usability you should have mentioned that. Regardless, more solutions are better then none. –  Aaron Lake Apr 26 '12 at 17:39
    
The most usable solution for the end user would be to create a binary that can call setuid and run as superuser and invoke the script file so the end user just double-clicks something and the desired result happens. There are security holes the size of Oklahoma in that solution, however, so this is a security enhancement on that one. –  Daniel Lawson Apr 26 '12 at 18:04
1  
@DanielLawson, I've created a text file with contents: do shell script "~/vpn_enable.sh" with administrator privileges. Then renamed it to .app file. When I double-click it I get: You can't open the application AppleScritpTest.app because the Classic environment is no longer supported. –  geotavros Apr 26 '12 at 20:10

The problem's that when you do it from the command line, what you're doing is starting bash under sudo, and then sending those next two commands to bash, not the original shell. (the sign being that you need to exit twice)

When you do it in a script, the bash command never exits, so the next two commands never run.

It's not as elegant as the AppleScript solution, but if you're going to do this as a script from the command line, the equivalent would be:

#!/bin/sh --
sudo bash -c 'echo "plugin L2TP.ppp">>/etc/ppp/options' 
sudo bash -c 'echo "l2tpnoipsec">>/etc/ppp/options'

If we didn't need the io redirection (the >> bit), we could just call the command directly via sudo without needing the sudo sh -c trick. (and note that I had to quote the argument to sh -c to keep it from running the echo as root, but the file appending as the original user.)

It will work as a .command file from the Finder, but it'll bring up a terminal window, asking for your password, and if entered correctly, will run the commands. (assuming you haven't recently authenticated for sudo ... if you have, it'll run through without prompting)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.