Syntax for running command on a remote host

Submitted by rmiddle on Sun, 07/06/2008 - 11:45

Syntax for running command on a remote host:
ssh [USER-NAME]@[REMOTE-HOST] [command or script]


* ssh: ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.
* USER-NAME: Remote host user name.
* REMOTE-HOST: Remote host ip-address or host name, such as
* command or script: Command or shell script is executed on the remote host instead of a login shell.


(A) Get disk information from a server called
$ ssh df -h

(B) List what ports are open on remote host
$ ssh netstat -vatn

(C) Reboot remote host:
$ ssh reboot

(D) Restart mysql server (please note enclosed multiple command line arguments using a single or double quotes)
$ ssh '/etc/init.d/mysql restart'

(E) Get memory information and store result/output to local file /tmp/memory.status:
$ ssh 'free -m' > /tmp/memory.status

(G) You can also run multiple command or use the pipes, following command displays memory in format of "available memory = used + free memory" :
$ ssh free -m | grep "Mem:" | awk '{ print "Total memory (used+free): " $3 " + " $4 " = " $2 }'


Category Tags
Linux ssh