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.
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:
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 firstname.lastname@example.org 'uptime'
If we want to see the machine details we can execute following command:
sshpass -p 'yourpassword' ssh email@example.com 'uname -a'
To check the disk usage we need to run following command:
sshpass -p 'yourpassword' ssh firstname.lastname@example.org '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 email@example.com '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 firstname.lastname@example.org '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.
Leave a comment
Sep 23, 2020, 2:38:00 PM | Anurag Srivastava
Sep 6, 2020, 5:20:33 PM | Anurag Srivastava
Jan 16, 2020, 11:43:29 AM | Anurag Srivastava
Jan 14, 2020, 10:22:34 AM | Anurag Srivastava
Jan 7, 2020, 7:15:34 PM | Anurag Srivastava
Jan 6, 2020, 7:30:13 PM | Anurag Srivastava
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Jul 16, 2018, 5:00:02 PM | Anurag Srivastava
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava