It is possible to execute a command on a remote system using the ssh command. It is best if you set up password-less login for SSH if you want to run this from a script. An example would be retrieving the amount of memory usage:
1 |
ssh user@myremotemachine free -m
|
This works well except that even after the free -m command is run, the SSH connection is not terminated. Thus, if this is used in a script it may cause the script to quit prematurely or introduce some other undesireable behavior. To ensure that the SSH connection is closed after running the commands, include the exit command after the commands you want to run remotely and enclose the entire thing in a heredoc:
1 2 3 4 |
ssh user@myremotemachine <<-EOF free -m exit EOF |
This will ensure that the free -m command runs remotely and then the SSH connection is terminated.





