In the home or work lab, I often have to connect to various devices that are either temporary or don’t support SSH keys. In my home lab, I typically set all the lab equipment with a standard username and password, which allows me to connect to them quickly. As almost all devices these days support SSH, I setup a bash function that acts as an alias allowing me to quickly connect to a device using SSH either from my Mac or Linux desktop.

Install sshpass on Mac

On Mac, the best way to install sshpass is to compile from source. Please visit, https://sourceforge.net/projects/sshpass/ to verify the latest version of sshpass available and update the URL and text in red appropriately. The instructions assume that you have xcode and command line tools installed that will allow for the sshpass binary to be compiled.

curl -O -L http://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz
tar xvzf sshpass-1.06.tar.gz
cd sshpass-1.06
./configure
make && make install

The above will install sshpass in /usr/local/bin

Install sshpass on Ubuntu

Use the command below to install sshpass on Ubuntu

sudo apt-get install sshpass
Install sshpass on CentOS

In order to install sshpass on CentOS, EPEL repo needs to be enabled. Use the command to enable EPEL repo and install sshpass on CentOS.

yum --enablerepo=epel -y install sshpass
Configure bash function

On Mac or Linux, add the following line to .bash_profile file under your home directory.

connect() { "sshpass -p PASSWORD ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null USERNAME@$1"; }

Replace PASSWORD and USERNAME with the one that you want to use. The above creates a function called connect, which can be used to connect to a device.

connect ip.address.of.host
print