ssh execution help needed.

I m writing a program to execute remote script .
(display remote file content)
Whenever i execute following statement it gives me an error.

$command="cat /root/vm.cfg"
ssh -ttq root@10.180.8.236 $command

However following script work find.

ssh -ttq root@10.180.8.236 cat /root/vm.cfg
 

I have added rsa key so that it should not ask for any password while doing ssh.

What is wrong with using variable in ssh.
What could be the issue here?

Please post the error output.

Your example should work. From your explaination it is not clear to me if you only have problem with variable substitution or something with your password/key since you do mention that too.
To use ssh without password prompt you'll have to create and exchange keys that have been generated without entering a password while doing so (as side note).

Thanks for your reply .
As i mentioned
this query work fine from command prompt

ssh -ttq root@10.180.8.236 cat /root/vm.cfg

Hi,

use double quotes for the command variable

ssh -ttq root@10.180.8.236 "$command"

Yes, I have read this.

Your answer does not help much:

$> VAR="ls -la /etc/hosts"
$> ssh somehost "${VAR}"
-rw-r--r-- 1 root root 1128 2010-03-01 13:45 /etc/hosts

But now I see your typo:

$command="cat /root/vm.cfg"

Leave the $ out.

It got executed .But My problem is i m not able to execute the same from script.
I think it has something to do with scope of the variable.

As script there is no difference. I asked 2 times already - here is the third and last time: Post your error output you get, thank you.
Guessing around and asking the same stuff all the time is getting tideous :wink:

$> cat mach.ksh
#!/usr/bin/ksh

VAR="ls -la /etc/hosts"
ssh somehost "${VAR}"
$> ./mach.ksh
-rw-r--r-- 1 root root 1128 2010-03-01 13:45 /etc/hosts

still not able to get the proper output.

Error: Unable to open config file: /root/vm.cfg

---------- Post updated 03-24-10 at 01:09 AM ---------- Previous update was 03-23-10 at 11:38 PM ----------

---------- Post updated at 01:12 AM ---------- Previous update was at 01:09 AM ----------

I have tried this but no success.

So on the 10.180.8.236 host, can you open this file? get any error on this opening?
May be this file locked by the remote host.Otherwise command must be work success :wink: