1

I want to ssh into a remote host and ping another IP from that host. However, with the following script, I am only able to ssh into that host.

#!/bin/sh

sshpass -p <password> ssh -t -o StrictHostKeyChecking=no <username>@<remote host>
/usr/sbin/ping <some IP>

Then, after existing from the remote host (back into my source host), the ping starts.

In other words, when my script is executed, a ssh connection is made into a remote host. Ping doesn't take place on THAT remote host. When I manually exit from the remote host, the ping starts again (from my original host).

How do I make it so that the ping starts from the remote host instead of the source host?

2
  • 1
    I'd down-vote you if I could @WinnieTigger.
    – mc0e
    Jun 8, 2018 at 11:12
  • use keys not sshpass. Jun 8, 2018 at 12:11

2 Answers 2

6

ssh takes a command to run on the remote machine as its argument, e.g.

ssh user@host ping ip-addr

This will execute ping ip-addr on the remote machine and then terminate the connection.

When you place the ping command on the next line, as in

ssh user@host
ping ip-addr

ssh will, by default, start an interactive shell on the remote machine. When that shell is terminated, the control is handed back to your script, and the ping command is run locally.

1
  • 3
    The key point being that the remote command is supposed to be an argument or arguments to the ssh command, as opposed to being a separate command which is sent to the local shell.
    – mc0e
    Jun 8, 2018 at 11:12
1

use single quotes for the commands if you want to run on a remote machine through ssh. will be handy to run multiple commands as well.

#!/bin/sh

sshpass -p <password> ssh -t -o StrictHostKeyChecking=no <username>@<remote host> '
/usr/sbin/ping <some IP>'
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .