# ssh execution help needed.

**URL:** <https://community.unix.com/t/ssh-execution-help-needed/262292>\
**Category:** Shell Programming and Scripting\
**Created:** [March 23, 2010, 7:17am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292 "2010-03-23T07:17:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinga123](https://community.unix.com/letter_avatar/pinga123/32/5_5575768a8748004e209b776fc1b2916d.png) [@pinga123](https://community.unix.com/u/pinga123)\
**Post date:** [March 23, 2010, 7:17am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/1 "2010-03-23T07:17:20Z")

</div>

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

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

```

However following script work find.

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [March 23, 2010, 7:34am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/2 "2010-03-23T07:34:45Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![pinga123](https://community.unix.com/letter_avatar/pinga123/32/5_5575768a8748004e209b776fc1b2916d.png) [@pinga123](https://community.unix.com/u/pinga123)\
**Post date:** [March 23, 2010, 7:39am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/3 "2010-03-23T07:39:12Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![ktrimu](https://community.unix.com/letter_avatar/ktrimu/32/5_5575768a8748004e209b776fc1b2916d.png) [@ktrimu](https://community.unix.com/u/ktrimu)\
**Post date:** [March 23, 2010, 7:42am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/4 "2010-03-23T07:42:54Z")

</div>

Hi,

use double quotes for the command variable

ssh -ttq [root@10.180.8.236](mailto:root@10.180.8.236) "$command"

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [March 23, 2010, 7:43am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/5 "2010-03-23T07:43:53Z")

</div>

Yes, I have read this.

Your answer does not help much:

```nohighlight
$> 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:

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

```

Leave the $ out.

---

<div class="post-metadata">

**Author:** ![pinga123](https://community.unix.com/letter_avatar/pinga123/32/5_5575768a8748004e209b776fc1b2916d.png) [@pinga123](https://community.unix.com/u/pinga123)\
**Post date:** [March 23, 2010, 7:53am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/6 "2010-03-23T07:53:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [March 23, 2010, 10:00am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/7 "2010-03-23T10:00:18Z")

</div>

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 😉

```nohighlight
$> 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

```

---

<div class="post-metadata">

**Author:** ![pinga123](https://community.unix.com/letter_avatar/pinga123/32/5_5575768a8748004e209b776fc1b2916d.png) [@pinga123](https://community.unix.com/u/pinga123)\
**Post date:** [March 24, 2010, 2:12am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/8 "2010-03-24T02:12:57Z")

</div>

still not able to get the proper output.

```plaintext
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 ----------

> [@ktrimu](#):
>
> Hi,
> 
> use double quotes for the command variable
> 
> ssh -ttq [root@10.180.8.236](mailto:root@10.180.8.236) "$command"

I have tried this but no success.

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/5_5575768a8748004e209b776fc1b2916d.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [March 24, 2010, 5:14am UTC](https://community.unix.com/t/ssh-execution-help-needed/262292/9 "2010-03-24T05:14:35Z")

</div>

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 😉
