Execute Commands on Remote Machines using sshpass


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

In this blog I am going to explain how sshpass can help us to increase productivity in our day to day system admin job. Although it is not a secure way to connect to the remote machine still we can use sshpass for connecting on intranet machines to run different commands to quickly get the results like checking 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 log in to a remote machine either by key-based login or using username and password. In this process we have to log in to the remote machine then run the command to get the desired result but if we can run the command on a 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 the remote machine directly from our machine and can get the result from a remote machine.
So this is the introduction of sshpass now let's 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 leave a comment. If you like this blog then please share.


Comments (0)

Leave a comment

Related Blogs

preview thumbnail
Introduction to Logstash

Dec 20, 2019, 11:38:31 AM | Anurag Srivastava

preview thumbnail
Importing MongoDB data into Elasticsearch

Mar 9, 2019, 8:20:38 AM | Anurag Srivastava

preview thumbnail
Importing MySQL data into Elasticsearch

Feb 9, 2019, 12:06:18 PM | Anurag Srivastava

preview thumbnail
Snapshot and Restore Elasticsearch Indices

Sep 16, 2019, 5:55:06 AM | Anurag Srivastava

preview thumbnail
Log analysis with Elastic stack

Jan 31, 2018, 6:11:29 AM | Anurag Srivastava

preview thumbnail
Creating Elasticsearch Cluster

Apr 6, 2019, 8:41:41 PM | Anurag Srivastava

Top Blogs

preview thumbnail
Configure SonarQube Scanner with Jenkins

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

preview thumbnail
Execute Commands on Remote Machines using sshpass

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

preview thumbnail
Importing MongoDB data into Elasticsearch

Mar 9, 2019, 8:20:38 AM | Anurag Srivastava

preview thumbnail
Importing MySQL data into Elasticsearch

Feb 9, 2019, 12:06:18 PM | Anurag Srivastava

preview thumbnail
Configure Jenkins for Automated Code Deployment

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

preview thumbnail
Deploying Angular code using Python script

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