executing commands in remote server using ssh

I have some commands which need to be executed in remote machine.

I have Linux Server from where I need to connect to Solaris server using ssh and then declare some variable over there and run some commands. I don't want to call a script which is present in Solaris server from Linux server rather I want to execute commands in remote machine. Please help asap.

ssh machine_name "ps -ef"

As you have mentioned about declaring variables and executing commands, this will help

ssh user@hostname "a=\"ls -l\"; $a"

But I have written many commands ,doing that will be very hectic .pls suggest some easy way

what do you mean "written many commands"

if you are having list of commands to be executed, then use a script, other wise using this ssh could be hectic.

many commands means i have written a script .I want instead of executing that script I want that commands present inside the script should be executed.

If you want so, then as i have posted, execute your commands one by one by seperating them with semicolon.

I think this is what you want. It's one inline script calling a ksh which has two parts, the one surrounded by double quotes (") gets expanded on the local machine, allowing remote $variables to be set, then the second part is surrounded by single quotes (') which does not get expanded locally so you can use remote variables such as the $file one shown.

I think it looks quite clean, readable and self contained and you can quite a lot of commands run in the single quote section.

echo "OUTPUT"
dir="/tmp"
mask="*.tar"
ssh -n $USER@$MACHINE /bin/ksh -c ":
        # this part gets expanded locally so use only to pass in variables
             dir='$dir'
     mask='$mask'
"'
     # This gets run remotely do not use single quotes
             echo "using single quotes ('\'') is tricky"
             echo "searchng: $dir; for $mask;"
     cd "$dir"
     pwd
             for file in $(ls -t $mask); do
                         echo "found: $file"
            done
'
OUTPUT
using single quotes (') is tricky
searchng: /tmp; for *.tar;
/tmp
found: Another.tar
found: Save.tar