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 used to run the 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 than on the remote machine then… :) For that we have sshpass which enables 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
  1. Ubuntu-based distribution
apt-get install sshpass

By executing the 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 the 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 let’s execute the command to perform the commands on a remote server.

To get the uptime details of a remote machine we can execute the 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'

#servers #automation #sshpass #ssh #remote-working

Execute Commands on Remote Machines using sshpass
1.80 GEEK