user57421
user57421

Reputation: 7911

how to connect to a remote server using ssh and getting the information

I'm trying to write a shell script using bash. I have multiple servers and each servers have multiple apps runnings on it. Each server also has specific app scripts to check/stop/start etc. All I want to do is that, do a ssh and connect to the remote server. Which I'm also able to do sucessfully and exceute the commands also..

In some instance I need to check some process status on a remote machine, the app sepecific scripts already does that. But using my ssh when i try to execute that script I dont get any info ( it gets executed but no info is passed ). How do i get the information from the remote host and display on the local host here.

Any help on this is really appreciated.

Regards, Senny

Upvotes: 4

Views: 16628

Answers (1)

Shawn Chin
Shawn Chin

Reputation: 86924

You can run remote commands and get results locally by passing the command as a string to ssh.

In your script, you can do:

CMD_OUT=$(ssh user@remote_host "/path/to/script argument")

The command will be run remotely and the output store in the CMD_OUT variable. You can then parse the output in your script to get the results you want.

To simplify usage of your script, you might want to set up passwordless ssh so you don't have to type your password each time the script tries to run a remote command.

Upvotes: 8

Related Questions