Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

on OSX I do

sudo route delete default
sudo route add default 192.168.178.1

to change my default gateway.

On running a sh file with these I am asked for a password every time. Which I run as

sh changegw.sh.

How do I pass my password before hand?

share|improve this question
    
Instead of passing your password to the script, you could invoke the script with sudo sh changegw.sh or run sudo bash to get a root shell, then run your script from there. Then you won't need sudo in the script. –  rob Mar 17 '12 at 21:51

2 Answers 2

For sudo there is a -S option for accepting the password from standard input. Here is the man entry:

-S          The -S (stdin) option causes sudo to read the password from
            the standard input instead of the terminal device.

This will allow you to run a command like:

echo myPassword | sudo -S ls /tmp
share|improve this answer
2  
The downside is that your password is now in the shell history. –  rob Mar 17 '12 at 21:45
    
@rob You can prevent this pretty easily with the ignorespace trick. stackoverflow.com/a/5750144/490592 –  Chad von Nau Mar 16 '13 at 3:29

While it is easiest to simply run the script as root, you could also create a group in the sudoers file that can run the program without a password, and add your username to that group.

I've never done this personally, though, and cannot recommend a specific line to add to your sudoers file.

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.