# SSH execute remote command (with jump)

**URL:** <https://community.unix.com/t/ssh-execute-remote-command-with-jump/273690>\
**Category:** Shell Programming and Scripting\
**Created:** [September 20, 2010, 10:32am UTC](https://community.unix.com/t/ssh-execute-remote-command-with-jump/273690 "2010-09-20T10:32:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evan](https://community.unix.com/letter_avatar/evan/32/6_22734026db12803ebb3e059622ac3534.png) [@Evan](https://community.unix.com/u/Evan)\
**Post date:** [September 20, 2010, 10:32am UTC](https://community.unix.com/t/ssh-execute-remote-command-with-jump/273690/1 "2010-09-20T10:32:17Z")

</div>

Hi all,  
I am facing the following issue:

Host A should execute a remote command (say pwd) on host B2.  
B2 is not directly reacheable but you have to connect from a to B1, then from B1 you can execute the command ssh user@B2 pwd.  
B1 and B2 are directly connected:

A =\> B1 =\> B2  
| ^  
|\_\_\_\_pwd\_\_|

Is there a way to use ssh from host A + single command to do the work?

Thank you very much for help.

Evan

---

<div class="post-metadata">

**Author:** ![vistastar](https://community.unix.com/user_avatar/community.unix.com/vistastar/32/2003_2.png) [@vistastar](https://community.unix.com/u/vistastar)\
**Post date:** [September 21, 2010, 4:43am UTC](https://community.unix.com/t/ssh-execute-remote-command-with-jump/273690/2 "2010-09-21T04:43:52Z")

</div>

Maybe this works:

```nohighlight
#!/usr/bin/expect
spawn ssh usr1@IP_of_B1 ssh usr2@IP_of_B2 pwd #pwd is the command you wanna execute on B2
expect "password"
send "passowrd_of_usr1\n"
expect "password"
send "passowrd_of_usr2\n"
expect eof
exit

```

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [September 21, 2010, 5:05am UTC](https://community.unix.com/t/ssh-execute-remote-command-with-jump/273690/3 "2010-09-21T05:05:14Z")

</div>

set ssh tunnel on server A1.

```nohighlight
ssh -f userB1@B1 -L 2000:B2:22 -N

```

then you can run :

```nohighlight
ssh -P 2000 localhost pwd

```

It will show B2 status

OR

On serverA

```nohighlight
ssh userB1@B1 "ssh userB2@B2 pwd"

```

need keyless

---

<div class="post-metadata">

**Author:** ![Evan](https://community.unix.com/letter_avatar/evan/32/6_22734026db12803ebb3e059622ac3534.png) [@Evan](https://community.unix.com/u/Evan)\
**Post date:** [September 21, 2010, 5:10am UTC](https://community.unix.com/t/ssh-execute-remote-command-with-jump/273690/4 "2010-09-21T05:10:13Z")

</div>

Hi vistastar,  
thank you for your suggestion.

Since I can't use expect I just tried to ssh twice:  
ssh userB1@B1 "ssh userB2@B2 pwd"  
I didn't thought about this solution, but it make sense (THANKS!!).

It works fine 😉

Thanks again,  
Evan
