• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

sshpass Command: Non-interactive Password Authentication with SSH

by admin

Overview

Linux system Admins normally login to the Linux servers either supplying a password or using key-based authentication. sshpass is a tool that allows us to automatically supply passwords to the command prompt so that automated scripts can be run as desired by users. sshpass supplies password to ssh prompt using dedicated tty, fooling ssh to believe that an interactive user is supplying a password.

Some of the common uses of sshpass

1. taking backups to a remote server
2. executing commands on systems at a specified time.

sshpass Installation

1. Centos Based distributions:

Setup the EPEL repository from https://fedoraproject.org/wiki/EPEL and then as root run:

# yum -y install sshpass

2. Ubuntu/Debain based distributions:

As root run:

# apt-get install sshpass

3. Compile & install from the source:

# wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz
# tar -zxvf sshpass.tar.gz 
# cd sshpass-1.05/ 
# ./configure 
# make 
# make install
# which sshpass
/usr/local/bin/sshpass

Getting Help

# sshpass -h
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

At the most one of -f, -d, -p or -e should be used

Example 1: supply password with ssh

# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no

Where:

password is the password of your server(ldap.thegeekdiary.com). ‘StrictHostKeyChecking=no‘ is used to control logins to machines whose host key is not known or has changed.

EXAMPLE:2 TO RUN SOME COMMAND ON THE REMOTE SERVER VIZ CHECKING UPTIME AND UNAME

Example 2: To run some command on the remote server

Lets try running 2 commands “uptime” and “uname” on the remote server using the sshpass commnad:

# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no "uptime;uname -a"

Sample Output:

18:49:34 up 21 days, 18:49,  3 users,  load average: 0.01, 0.00, 0.00
Linux ldap.thegeekdiary.com 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux

Example 3: Copying a file using rsync to a server

In our case we are copying a file sshpass.tar.gz to a remote server ldap.thegeekdiary.com

# sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@ldap.thegeekdiary.com:/tmp/

Output of above Command:

sending incremental file list
sshpass.tar.gz
98362 100% 62.56MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 98472 bytes received 31 bytes 197006.00 bytes/sec
total size is 98362 speedup is 1.00
root@server1:/home/thegeekdiary#

Example 4: for loop for copying on to remote servers

Create a file as the following:

# touch /tmp/scr

The file should contain the name of the hosts:

server2.thegeekdiary.com
server3.thegeekdiary.com
server4.thegeekdiary.com
server5.thegeekdiary.com
# for i in `cat /tmp/scr`; do echo " ";echo "###$i####"; sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@$i:/tmp/; done

Output of Above Command:

###server2.thegeekdiary.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33

###server3.thegeekdiary.com####
sending incremental file list
sent 54 bytes received 12 bytes 44.00 bytes/sec
total size is 98362 speedup is 1490.33

###server4.thegeekdiary.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33

###server5.thegeekdiary.com####
sending incremental file list
sent 54 bytes received 12 bytes 132.00 bytes/sec
total size is 98362 speedup is 1490.33

Filed Under: Linux

Some more articles you might also be interested in …

  1. pacman-mirrors Command Examples in Linux
  2. find Command Examples in Linux
  3. pidstat: command not found
  4. iwctl: command not found
  5. CentOS / RHEL 7 : How to configure cache-only nameserver
  6. Configuring sudo to Enable Commands for Non-Root Users in Linux
  7. mpg123: command not found
  8. Understanding rsyslog Actions
  9. sysctl Command Examples in Linux
  10. auracle Command Examples

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright