thedark Second Lieutenant
Joined: 30 Jul 2005
Posts: 1074
|
Posted: Sun Jul 31, 2005 10:18 am Post subject: Running commands on a Remote host |
|
|
Eventually we would like to execute a command on a remote Linux/FreeBSD/Solaris/UNIX host and have the result displayed locally. Once result obtained it can be used by local script or program. Here are few examples:
File system and disk information
User information
Particular service is running or not and so on..
You can use rsh or ssh for this purpose. However, for security reason you should always use the ssh and NOT rsh. Please note that remote system must run the OpenSSH server.
Syntax to running command on a remote host:
ssh [USER-NAME]@[REMOTE-HOST] [command or script]
Where,
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 fbsd.cyberciti.biz.
command or script: Command or shell script is executed on the remote host instead of a login shell.
Examples:
(A) Get disk information from server called www1.cyberciti.biz
$ ssh vivek@www1.cyberciti.biz df -h
Password:
(B) List what ports are open on remote host
$ ssh vivek@www1.cyberciti.biz netstat -vatn
(C) Reboot remote host
$ ssh root@www1.cyberciti.biz reboot
(D) Restart mysql server (please note if you have multiple argument to command then enclosed them in single or double quote)
$ ssh root@www1.cyberciti.biz '/etc/init.d/mysql restart'
(E) Get memory information and store result/output to local file /tmp/memory.status:
$ ssh vivek@www1.cyberciti.biz '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 vivek@debian.test.com free -m | grep "Mem:" | awk '{ print "Total memory (used+free): " $3 " + " $4 " = " $2 }' |
|