Run Remote Commands with SSH
October 25th, 2006
The ssh command is an amazing program. You can use it for opening sessions between server or you can use it run a command on a remote system, non-interactively. A simple example of which might be getting file system usage:
root@admin:~ # ssh root@www 'df -h'
Filesystem Size Used Avail Use% Mounted on
/dev/ubda 3.5G 2.1G 1.4G 61% /
tmpfs 96M 4.0K 96M 1% /dev/shm
This is cool, but is not extremely useful. It gets really cool when you use SSH public/private key pairs to automate tasks on many servers. I will cover that in the future.
Until then, you can still do some pretty cool stuff, like count the number of webserver proccess running on your webserver. Without actually logging into the box and typing the command. All you have to do is place the command in quotes after the base ssh command. The following connects to the server www as root and then pipes the process table to grep apache. It then pipes that to grep -v grep (this eliminates the process grep apache itself and leaves the apache processes) and finally to wc -l which counts the number of lines, in this case the number of apache processes.
root@admin:~ # ssh root@www 'ps -ef | grep apache | grep -v grep | wc -l'
11
Or display a systems vital stats and highest CPU utilizing process:
root@www:~ # ssh root@www 'top -b -n 1 | head -n 8'
top - 11:01:04 up 20 days, 6:47, 1 user, load average: 0.44, 0.21, 0.09
Tasks: 60 total, 1 running, 59 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.7% user, 1.5% system, 0.0% nice, 96.8% idle
Mem: 195620k total, 186276k used, 9344k free, 16380k buffers
Swap: 525304k total, 516k used, 524788k free, 26912k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7399 root 19 0 936 936 740 R 9.2 0.5 0:00.20 top
Or see who is logged in on a system:
root@www:~ # ssh root@www 'who'
brock pts/0 Oct 21 10:31 (75.72.194.149)
jim pts/1 Oct 25 06:25 (128.101.163.128)
If you would like to be even more amazed or amused read my article on setting up ssh keys. SSH keys allow you to
skip entering a password, like I did above, when running commands on remote systems.


October 24th, 2007 at 4:51 pm
[…] quite easy to achieve (with pubkeys so you don’t have to enter passwords everytime). Run Remote Commands with SSH should get you started. However, there are much better tools for monitoring multiple boxes out […]
November 17th, 2007 at 7:51 pm
hi….
i used ssh to halt another user …after i entered thru ssh…. i entered the root …. then typed halt….
the problem is that the system wont start….
i cannt figure out why……
please help me to start this system in normal mode…
both systems have ubuntu(gutsy)
November 28th, 2007 at 10:25 am
Very helpful — thanks a lot for putting this up
April 2nd, 2008 at 11:45 am
“i used ssh to halt another user”
how do you halt a user???
you have probably have shut down your machine. push the power button. you can’t taht remotely. (unless it has WakeOnLAN, even then you need access to another machine in the same subnet)
May 29th, 2008 at 7:41 am
[…] install the key on a remote host. If the command below looks daunting, read the following articles Run Remote Commands with SSH and Standard Error and Standard Out . brock@www:~/.ssh$ cat id_rsa.pub | ssh mysql105 ‘cd […]
June 10th, 2008 at 1:13 am
Hi,
How can I run “ssh” command from Windows prompt to connect to the Server (solaris)?
I mean, do I need to install any software to run SSH command in Windows command prompt and classpath settings required. Please suggest me… and can mail me to this ID as well (vaddamani.prasanna@yahoo.co.in)
Regards,
Prasanna
July 2nd, 2008 at 3:39 am
I’d suggest doing:
ssh root@www ‘ps -ef’ | grep apach[e]| wc -l’
* Firstly, you connect, do a ps, then process the text on your machine
* No need for “grep -v grep” by expanding one of the letters of what you’re grepping.
- John
July 2nd, 2008 at 10:56 am
John,
That’s a novel solution. Thanks for sharing!
July 16th, 2008 at 2:27 pm
Its gets much more interesting to start including ssh commands in scripts.I have a script which I run on one server which uses ssh to connect and execute scripts I have put on each of my 23 other servers.
This basically takes the work out of my job.
August 1st, 2008 at 3:25 pm
There’s also no need for the pipe to wc -l, its more efficient to use grep -c.
So in effect:
ssh root@www ‘ps -ef’ | grep -c apach[e]
August 6th, 2008 at 8:19 am
How can i stop the execution of commands.
for eg: if a user from system1 run a command like ssh user@system2 rm *, then it will delete all the files of the user in system.
How can i stop the execution of commands through ssh.
August 7th, 2008 at 4:09 pm
Another option is to put it all in a .sh file on the host and just run that.