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?
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
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.
sudo sh changegw.shor runsudo bashto 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