«

»

sshpass configuration & usage

An ongoing project is very Linux oriented and i was given a CentOS based VM by the customer to use while delivering the project. This blog post will provide some tips to make administration a bit easier. Yes i know that you might not want to put password in a text file, which i do in one of the scenarios, but that is completely up to you to decide if it is acceptable or not.
What i wanted to achieve is pretty much same thing as for my home lab meaning being able to run SSH sessions without prompted for a password everytime i want to either connect to a remote SSH session or run remote commands. The below procedure covers what you need to do if using sshpass and it also includes some .bashrc configuration. Target environment is obviously a Nutanix cluster meaning the targets have the same password within a Nutanix cluster. Targets in this case are:

  • Controller Virtual Machine (CVM)
  • AHV host (Nutanix hypervisor)

sshpass configuration

  1. Make sure your CentOS based VM connects to a download repo that holds the sshpass binary by running the following command:
    1. cd /etc/yum.repos.d/
    2. wget http://download.opensuse.org/repositories/home:Strahlex/CentOS_7/home:Strahlex.repo
      screen-shot-2016-12-22-at-09-12-21
  2. Install sshpass using the following command:
    1. yum -y install sshpass

sshpass usage

Now you’re ready to use sshpass and i’ll describe two scenarios:

  1. Add command aliases in your .bashrc file
  2. Running remote command to multiple targets

bashrc

Update the .bashrc file with your requirements. The file is located in the user home directory. My .bashrc file looks like:

alias ssh="ssh -o  ServerAliveInterval=15 -o CheckHostIp=no  -o TCPKeepAlive=yes -o LogLevel=quiet -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
# Alias to connect to any system using the nutanix user account with the below specified password
alias nssh="sshpass -p secretpassword $ssh -l nutanix"
# Alias to connect to any system using the root user account with the below specified password.
alias rssh="sshpass -p secretpassword $ssh -l root"

 

When done you simply run:

  • “nssh <IP/FQDN>” to connect to e.g. a CVM:
    screen-shot-2016-12-22-at-09-17-43
  • “rssh <IP/FQDN>” to connect to a root user based session. In my case an AHV host:
    screen-shot-2016-12-22-at-09-21-48

If you don’t want to use a text file including the password you can the following sshpass command:

  • sshpass -p secret password <IP/FQDN> -l username

Remote command

If you want to run commands against multiple targets you can e.g. use a text file to specify all remote targets and loop through the file when running your command.
In the below example i’ll show a simple for loop running the command “df -h” on each remote system.

  1. Create a text file, rtargetspasswd, and specify the password require.
  2. Create a file containing the remote systems. In my case i created a file called rtargets and included the following systems:
    1. 10.20.200.131
      10.20.200.132
      10.20.200.133
      10.20.200.134
      10.20.200.135
      10.20.200.136
      10.20.200.137
      10.20.200.138
  3. Run command
    1. Use the below command to run “df -h” against all remote targets specified in the rtargets file:
      1. for i in `cat rtargets` ;do /usr/bin/sshpass -f rtargetspasswd ssh $i df -h  ; done
    2. In my case i was interested in the utilisation of a specific partition, /data01, in each remote target so i used this command to reduce the output:
      1. for i in `cat rtargets` ;do /usr/bin/sshpass -f rtargetspasswd ssh $i df -h | grep -i data01 ; done
        screen-shot-2016-12-22-at-09-29-02