Remote command in variable using ssh

Hi, I've an issue in a shell script:

I'm opening an ssh connection to a remote server, then I want to store the result of a ls command in a variable, but it doesn't work: the ls is done on the local machine.

ssh user@server << EOF
 ls # works as expected (ls is done remotely)
 test=`ls` # the ls is done locally and not remotely
EOF

Note: I have to use EOF, because they are several others commands done during the ssh.

any solution for this problem ?

Try

test=\`ls\`

Or try

ssh user@server << 'EOF'

This treats the following text as being in 'ticks'
But often this replaces one problem by another.
The real solution is to have two scripts:

#localscript
ssh user@server < remotescript

Or, safer, force the correct interpreter in a remote shell:

#localscript
ssh user@server /bin/sh < remotescript

No restriction on remotescript , and can be interactively tested on a remote system.