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
- Make sure your CentOS based VM connects to a download repo that holds the sshpass binary by running the following command:
- Install sshpass using the following command:
- yum -y install sshpass
sshpass usage
Now you’re ready to use sshpass and i’ll describe two scenarios:
- Add command aliases in your .bashrc file
- 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:
- “rssh <IP/FQDN>” to connect to a root user based session. In my case an AHV host:
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.
- Create a text file, rtargetspasswd, and specify the password require.
- Create a file containing the remote systems. In my case i created a file called rtargets and included the following systems:
- 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
- 10.20.200.131
- Run command
- Use the below command to run “df -h” against all remote targets specified in the rtargets file:
- for i in `cat rtargets` ;do /usr/bin/sshpass -f rtargetspasswd ssh $i df -h ; done
- 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:
- Use the below command to run “df -h” against all remote targets specified in the rtargets file: