4

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?

epsilon8
  • 730
  • 3
  • 12
  • 22
  • 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 Answers2

10

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
Lucas Kauffman
  • 2,680
  • 6
  • 20
  • 26
0

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.

Babu
  • 1,347