Running Commands on a Remote Linux / UNIX Host

by on July 25, 2005 · 17 comments· LAST UPDATED January 29, 2008

in , ,

You 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. A few examples:
=> File system and disk information

=> Get user information

=> Find out all running process

=> Find out if particular service is running or not etc

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 for 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 a server called www1.cyberciti.biz:
$ ssh vivek@www1.cyberciti.biz df -h

(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 enclosed multiple command line arguments using a single or double quotes)
$ 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 }'

See how to configure ssh for password less login using public key based authentication.

=> Related: shell script to get uptime, disk usage, cpu usage, RAM usage,system load,etc. from multiple Linux servers and output the information on a single server in a html format.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 17 comments… read them below or add one }

1 Amitabh May 14, 2008 at 10:23 am

Hi ,

Can you please help to show “how to run top command in a remote host” ?

Thanks,

Amitabh

Reply

2 web designer July 22, 2009 at 9:38 am

I have just put this in a script in a cron job to keep the mysql databases in sync. Great.

Reply

3 web design July 22, 2009 at 9:42 am

Hi Amitabh, Have you tried “ssh root@domain.com htop”.

Im not sure where the output would go but its worth a try if you havent already.

Lien

Reply

4 web design and seo August 4, 2009 at 10:07 pm

Thank you, Ive been trying to do this for weeks

Reply

5 Sandy December 24, 2009 at 5:42 am

Hi,

I am trying to execute the script from my local to remote host.
ssh sandy “/home/user/san/print_my_lines”

The scripts works all right but not able to come out of remote host.

Could you please help me out.

Reply

6 Sandy December 24, 2009 at 5:44 am

continue to above post.

Local host – aaa
RemoteHost – sandy

Reply

7 venkat June 25, 2010 at 9:24 pm

dude…I want to execute the following

ssh user@server

it is asking for password….how to give password dynamically?

Reply

8 Pramod September 22, 2010 at 12:57 pm

You need to exchange the ssh keys between the remote and your machine from where you are doing this.

Reply

9 sarath June 27, 2011 at 8:04 am

Is any additional configurations are required to use ssh in Ubuntu

Reply

10 Raj August 26, 2011 at 6:04 am

How can we run a script on a remote machine without passwords?

When i issue the following command, it is prompting for passoword. Could any one advise on this please ?
ssh username@hostname df -h

Reply

11 Siddhat March 5, 2012 at 10:17 am

Can you please suggest how to check the system uptime for last 1year in Unix envirnment is there any parameter to check with uptime cammand which can show the logs of approximately 1 year

Warm Regards ,

Siddhant K

Reply

12 saurabh July 6, 2012 at 12:34 pm

hi , can any one tell me hw i can enter the password dynamically

Reply

13 Thyagaraj September 27, 2012 at 8:56 am

For all who need password automation:

There is a package called “sshpass” for password automation, you can search if it’s available in the Ubuntu repository by typing ‘apt-cache search sshpass’.

You can install ‘sshpass’ by ‘apt-get install sshpass’.

Using ‘sshpass':
sshpass -p remote-password-here ssh -o StrictHostKeyChecking=no remote-user-here@remote-ip-here < /path/to/script
(or)
sshpass -p secret ssh -o StrictHostKeyChecking=no root@192.168.1.2 "ls -l"

Reply

14 rubence September 22, 2013 at 12:44 pm

hello, how can i execute a script in remote machine keeping the script in local machine

Reply

15 rubence September 22, 2013 at 12:51 pm

got it …

for refrence

ssh root@remotemachine sh < local_script.sh

Reply

16 maonato October 8, 2013 at 9:58 am

hi,, i want to run mu script to a server, the script is in add file
will this command “ssh -i id_rsa@10.0.103.96 ‘bash -s’ > ~.ssh/authorized_keys
exit

Reply

17 tigerwhat January 20, 2014 at 2:12 am

Anyone have an idea of how to use this in with except/spawn?

Reply

Leave a Comment

Tagged as: , , , , , , ,

Previous post:

Next post: