Linux: how to install and use sshpass


Installation:

Example for CentOS:

[root@tutorialspots ~]# yum install -y sshpass
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                     |  13 kB     00:00
 * base: linux.darkpenguin.net
 * epel: mirror.imt-systems.com
 * extras: mirror.checkdomain.de
 * updates: mirror.fra10.de.leaseweb.net
base                                                     | 3.6 kB     00:00
epel                                                     | 4.7 kB     00:00
extras                                                   | 2.9 kB     00:00
ius                                                      | 1.3 kB     00:00
nodesource                                               | 2.5 kB     00:00
updates                                                  | 2.9 kB     00:00
(1/3): updates/7/x86_64/primary_db                         | 2.9 MB   00:00
(2/3): epel/x86_64/primary_db                              | 6.8 MB   00:00
(3/3): epel/x86_64/updateinfo                              | 1.0 MB   00:00
Resolving Dependencies
--> Running transaction check
---> Package sshpass.x86_64 0:1.06-2.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package           Arch             Version              Repository        Size
================================================================================
Installing:
 sshpass           x86_64           1.06-2.el7           extras            21 k
 
Transaction Summary
================================================================================
Install  1 Package
 
Total download size: 21 k
Installed size: 38 k
Downloading packages:
sshpass-1.06-2.el7.x86_64.rpm                              |  21 kB   00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : sshpass-1.06-2.el7.x86_64                                    1/1
  Verifying  : sshpass-1.06-2.el7.x86_64                                    1/1
 
Installed:
  sshpass.x86_64 0:1.06-2.el7
 
Complete!

Usage

sshpass can accept password – as an argument, read it from file or via environment variable.

Method 1: Password as an argument

sshpass -p yourpassword ssh -o StrictHostKeyChecking=no -o LogLevel=quiet root@ip command

Example:

[root@tutorialspots ~]# sshpass -p yourpassword ssh -o StrictHostKeyChecking=no -o LogLevel=quiet root@tutorialspots.com 'free -m'
              total        used        free      shared  buff/cache   available
Mem:          31923         888         471           0       30563       30643
Swap:         32733           5       32728

Method 2: Password from file

sshpass -f password-file ssh -o StrictHostKeyChecking=no -o LogLevel=quiet root@ip command

Method 3: Password from environment variable

export SSHPASS="your-password"
sshpass -e ssh -o StrictHostKeyChecking=no -o LogLevel=quiet root@ip command

Leave a Reply