Blog Detail

preview image DevOps
by Anurag Srivastava, Jul 16, 2018, 5:00:02 PM | 4 minutes

Execute Commands on Remote Machines using sshpass

In this blog I am going to explain how sshpass can help us to increase the productivity in our day to day system admin job. Although it is not a secure way to connect to remote machine but we can use sshpass for connecting on intranet machines to run different commands to quickly get the results like check the uptime of a remote machine, check the disk usage of a remote machine or check the software version of a machine etc.

Typically we use to run ssh command to login to a remote machine either by key based login or using username and password. In this process we have to login to the remote machine then run the command to get the desired result but if we can run the command on remote machine from our own machine and can get the result on the same machine rather that on the remote machine then.... :) For that we have sshpass which enable this functionality.


Using sshpass we can execute the command on remote machine directly from our machine and can get the result from remote machine.
So this is the introduction of sshpass now lets move forward and start implementing it but before that we need to install sshpass.


Install sshpass:
I am going to explain how we can install sshpass on CentOS based distribution and Ubuntu based distribution.
1) CentOS based distribution

yum -y install sshpass

2) Ubuntu based distribution

apt-get install sshpass


By executing above command we can install sshpass on our machine. After installing sshpass we can see the help related to sshpass by typing following command:

sshpass -h


Which gives us following response:

Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
   -f filename   Take password to use from file
   -d number     Use number as file descriptor for getting password
   -p password   Provide password as argument (security unwise)
   -e            Password is passed as env-var "SSHPASS"
   With no parameters - password will be taken from stdin

   -h            Show help (this screen)
   -V            Print version information


By seeing the help command response you may have an idea how we can execute the command. Now lets execute the command to perform the commands on remote server.

To get the uptime details of a remote machine we can execute following command:

sshpass -p 'yourpassword' ssh user@192.168.12.xx 'uptime'


If we want to see the machine details we can execute following command:

sshpass -p 'yourpassword' ssh user@192.168.12.xx 'uname -a'


To check the disk usage we need to run following command:

sshpass -p 'yourpassword' ssh user@192.168.12.xx 'df -h'


In this way we can execute any linux command directly on remote machine and can get the result on our machine. These commands will show the results on screen but if we want to write it on a file we can do that by simply providing the greater that symbol to redirect the output to a file:

sshpass -p 'yourpassword' ssh user@192.168.12.xx 'df -h' > /var/tmp/disk_usage


We can also execute multiple commands at a time by adding them using the semi colon:

sshpass -p 'yourpassword' ssh user@192.168.12.xx 'uptime;uname -a;df -h' 


In this way using sshpass we can fetch details from different remote servers and can write them to a file so that we need not to login to different machines for getting the required details. By adding these commands in cron we can automate the day to day activity. It is not recommended to use this command on a production machine.

If you have any query then please let me know. If you like this blog then please share.

About Author

Anurag Srivastava

Author | Blogger | Tech Lead | Data Scientist | Innovator |

View Profile

Comments (0)

Leave a comment

Related Blogs

Load csv Data into Elasticsearch

Oct 15, 2018, 6:25:43 PM | Anurag Srivastava

htop: An Interactive Process Viewer

Oct 13, 2018, 8:49:59 PM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Build and deploy Angular code using Python

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Top Blogs

Build and deploy Angular code using Python

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava