This sounds super easy. Just echoing some lines into root owned file using using sudo. You figure something like "sudo echo blah >> /tmp/test" would work fine. You would be wrong. Let me demonstrate. You need sudo access to the root user to do this. Here is an example of the problem:
user@host$ touch /tmp/test user@host$ sudo chown root: /tmp/test user@host$ ls -la /tmp/test -rw-r--r-- 1 root root 0 2011-07-16 14:34 /tmp/test user@host$ sudo echo blah >> /tmp/test bash: /tmp/test: Permission denied
The echo program is running as root because we used sudo, but the shell that's redirecting echo's output to the root owned file is still running as the unprivileged user. The current shell does the redirection before sudo starts. To fix this we need to run the whole pipeline under sudo.
user@host$ echo "echo blah >> /tmp/test" | sudo bash user@host$ cat /tmp/bash blah
Doing this as an ssh command to another machine adds some more complication but to get it to work use the following command. This works best if you have ssh keys setup and NOPASSWD access to sudo to root. On machines with remote root login disabled you are forced to use sudo to perform ugly commands like the following.
ssh user@host 'sudo su root -c "echo \"echo test >> /tmp/test\" | sudo bash"'
The above command uses "sudo su root" because of an issue I saw with OpenBSD. If you did not put in the root user then you would get the error "su: no such login class:". Putting in the root user fixes this issue.
If you have more than one line to execute on a remote server then I would suggest just putting all of the commands you want to execute into a script and copy the script to the machine with scp and then executing with another ssh command. This saves you from executing that nasty ssh line above and writing a whole script filled with ssh commands.