There are a few ways to do it, but each service that you're trying to authenticate with will be different.
SSH - SSH is really easy if you use keys, instead of passwords. Here's a simple howto for that:
You can also use Perl::Net::SSH if you can script in perl, but this option requires having the password listed in plain text in the script.
Mysql - Mysql also requires having the password in plain text in order to script anything, but it's really simple to use a seperate, secure file so that you don't accidentally give out your password if you share the script:
First, the plain command-line way:
# mysql -u'user' -p'password' dbname -e 'insert query here'
Next, the external file way:
# mysql -u'user' -p`cat /path/to/file` dbname -e 'insert query here'
(the `backticks` are important!)
su - There is an alternative that can be used without a password, and it's called "sudo". This is always a dangerous option, but if you have a user that you would like to be able to use to execute scripts with sudo priveledges, you can use visudo to add the following line to the sudoers file:
username ALL=(ALL) NOPASSWD: ALL
You can even restrict the user to only certain commands. I use the above on my desktop system because I get tired of typing my passwd every time I want to "su" to root ;-P
I hope this helps!