Script to connect to a remote server and execute scripts

Hello All

I need a script or set of commands which can establish a remote connection with another server and execute some scripts over there. Basically it has to establish the connection with the remote server as an user ,say 'testuser' and then execute the script 'testscript'. and return the output to the parent server.

Any help is highly appreciated
-Thanks

SSH + Public Key Authentication. Both are very well documented in various threads and on the Web.

My idea is to execute the script from say 'host 1' which can do the job of whatever said above in 'host 2' and return

...plus, testscript has to be on remote server, then you can get the output. But you can't execute a local script on remote host via ssh.

I'm looking for a shell/perl or even a batch of unix commands to accomplish the same

You can't just say "this script will run on another host". The script has to be locally available on the other host, either by copying it there or by a mounted remote share.

As a starting point...
1.sh:

#!/usr/bin/ksh

user=$1
server=$2

echo $user
echo $server

echo 'for i in 1 2 3 4 5
do
    echo $i
done' | ssh ${user}@${server}

cat 2.sh | ssh ${user}@${server}

scp 2.sh ${user}@${server}:/tmp/. && ssh ${user}@${server} 'chmod u+x /tmp/2.sh && /tmp/2.sh'

2.sh:

for i in 1 2 3 4 5
do
    echo $i
done

Run as './1.sh <user> <server>'. This will connect to the remote server and run the same code in 3 different variants. Choose the one that fits you best.