The UNIX program expect is a tool for automatic interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc. expect is also useful for testing these same applications. Expect reads the output from interactive applications, allowing scripts to act based on the contents of that output.
With this very handy tool, you can easily create scripts that act on resulting output from commands. I created a cron job as an ordinary user calling a script to repair the privileges on my root disk (for which you must have root access). You might also create a root cron job for this, but I used this example to show you how to script root commands as an ordinary user. The script will insert your root password automatically when it is prompted for this!
This is what to do:
#!/sw/bin/expect -fYou must replace "yourname" with the name of your root login account, 12.34.56.78 with your ip number and "yourpassword" with your root password. Of course, you must have ssh enabled to be able to login with ssh into your superuser account. The script will wait for the literal output that is in the "expect" lines, so you might have to check if this output is the same on your system.
spawn ssh -2 -l yourname 12.34.56.78
expect "yourname@12.34.56.78's password:"
send "yourpassword\r"
expect "yourname%"
send "sudo diskutil repairPermissions / \r"
expect "password:"
send "yourpassword\r"
set timeout 240
expect "volume."
40 20 * * * theFullPath/repairprivsbootReplace theFullPath with the full path to the script. This line will start the root disk repair of privileges every evening at fourty minutes past eight.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20021126055408382