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.
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 let me know. If you like this blog then please share.
Author | Blogger | Tech Lead | Elastic Stack | Innovator |View Profile
Leave a comment
Sep 16, 2019, 5:55:06 AM | Anurag Srivastava
Oct 13, 2018, 8:49:59 PM | Anurag Srivastava
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Apr 6, 2019, 8:41:41 PM | Anurag Srivastava
Apr 14, 2018, 1:18:05 PM | Anurag Srivastava
Jan 31, 2018, 6:11:29 AM | Anurag Srivastava
Feb 9, 2019, 6:34:22 PM | Anurag Srivastava
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Jun 26, 2018, 4:50:18 PM | Anurag Srivastava
May 12, 2018, 4:47:07 PM | Anurag Srivastava
Jul 16, 2018, 5:00:02 PM | Anurag Srivastava
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
Jun 2, 2018, 10:49:54 AM | Anurag Srivastava
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
May 26, 2018, 6:42:02 PM | Anurag Srivastava