Communicating between 2 Raspberry Pi’s using sshpass

By Renier Delport
Published: 10 September 2016
Filed under:

In my home automation project, I sometimes need to run (background) commands from my web server towards another Raspberry Pi. Communicating between 2 Raspberry Pi’s using sshpass was the best way to do this using Bash commands. Last updated: November 24, 2016 at 8:52 am

sshpass is a Linux command that uses the ssh protocol to create short, direct terminal link between two computers. It basically allows you to access/run files locally on a remote computer. Although it seems like the command is running locally, it is actually executed remotely.

sshpass allows to run short commands/scripts that executes fast (and stops) and is great for running fast script files (Bash or Python – .sh or .py). Alternatively longer or continual commands, like for example playing internet radio, can be run in a screen session.

Apart from the sshpass command, this tutorial will also show you how to install the required package and create authenticity between the Pi’s as to be able to run the command without having to supply the password every time in the future.

In my case, both Pi’s are running the popular Raspbian operating system and is connected to a local area network (LAN). I have access to both of them through a PuTTY terminal and a static IP (recommended), but the setup can also be done individually without static IPs. Once the once off setup is complete, the sshpass commands can be run on their own.

In order to also get familiar with the directory structure of the system files that are created by Raspbian, I’ve also included steps to follow the process as we go. This will enable you to understand and do an appropriate backup and restore of these files and settings.

Setting up the Raspberry Pi’s to use sshpass

Remote vs. Local

Remote and local will refer to what we want to use the Pi’s for after the setup is complete, even though technically both will be accessed remotely if you for example use PuTTY. The remote Pi will be the Pi where the commands are ultimately executed on, whereas the local Pi will be the Pi where the commands are triggered from.

The setup process is fairly easy and only has to be done once.

SSH System Files

In order for Raspbian to remember which remote users can be trusted or not, the authorised settings will be saved in the form of 4 files in the /home/pi/.ssh directory. If no hosts are authorised yet, (i.e. on a freshly installed Raspbian) the local Raspberry Pi won’t have the directory created yet.

To make sure, from a freshly booted terminal, use

cd /home/pi/.ssh
dir

and you will see a “-bash: cd: /home/pi/.ssh: No such file or directory” error (continue reading).

If you have this directory available and there are files present, for now I’m going to recommend that you start fresh by making a backup of these files and removing the original ones. Use:

cd /home/pi
mkdir /home/pi/sshbackup
sudo cp -rp /home/pi/.ssh /home/pi/sshbackup
sudo rm -r /home/pi/.ssh

To create the /.ssh directory (either after you removed it or it wasn’t there to start off), use:

install -d -m 700 ~/.ssh

from the /home/pi directory.

The Remote Raspberry Pi

Apart from the remote Pi’s username and password, you will need three things:

  1. The server IP address hostname -I
  2. The hostname hostname
  3. A file to copy to the future local Pi to establish authenticity

In my case the remote username will be pi and the password will be raspberry
For explanation purposes, the IP of the server will be 192.168.1.xx and the hostname will be hostname.

Log into your remote computer Pi’s terminal using your username and password. By default you will be in the /home/pi directory. Now we need to create a text file in a known directory that is on both the host and server Raspberry Pis. Let’s sommer use /home/pi as this directory. To create a quick text file use:

nano /home/pi/hostname.txt (use your own server hostname) and place the following text into it:

To get this file and enable key
sudo scp [email protected]:/home/pi/hostname.txt /home/pi/hostname.txt
If this file is on another Pi, the key is probably already enabled.

Press Ctrl + X, then y to save your file.

The local Raspberry Pi

Log into your local terminal using your local Pi’s username and password pi and raspberry
Use:
sudo apt-get install sshpass
to install sshpass.

Then, to create authenticity of the remote Pi we will copy the text file we just created to the remote Pi to the host Pi:
sudo scp [email protected]:/home/pi/hostname.txt /home/pi/hostname.txt

Authenticity might already be establish, but in the likely event that it is not, you will see the following message:

The authenticity of host '192.168.1.xx (192.168.1.xx)' can't be established.
ECDSA key fingerprint is 1f:cd:15:39:... .
Are you sure you want to continue connecting (yes/no)?

Type yes
Then you will see:

Warning: Permanently added '192.168.1.xx' (ECDSA) to the list of known hosts.
[email protected]'s password:

Type the password for the host. In this case it’s raspberry. The file will be copied and the authentication of the remote Pi will be established.

Using sshpass between two Raspberry Pi’s

Now you can use sshpass as follows:

sudo sshpass -p 'raspberry' ssh [email protected] 'your command here'

where ‘raspberry’, pi and 192.168.1.xx is the password, username and IP address of your remote Pi. You should be able to use sudo commands as well, but if not, please come back to me by commenting below.

Conclusion

I use sshpass to run many small scripts from one Raspberry Pi onto another. Note that the process needs to be repeated to be able to run sshpass the other way around (from the original remote Pi to the host Pi).

Once you’ve set everything up it is quite easy to use for example in Bash and Python coding.

Please Rate, Share, Comment & Disqus

Was this Post Helpful?

(2 reader(s) found this helpful)
You have not rated this post yet. You can do so clicking on the thumbs up icon.
Loading...

You Might Also Like

Summary

Communicating between 2 Raspberry Pi’s using sshpass
In my home automation project, I sometimes need to run (background) commands from my web server towards another Raspberry Pi. Communicating between 2 Raspberry Pi's using sshpass was the best way to do this using Bash command.
www.behind-the-scenes.co.za

Comment via Facebook

comments so far

Disqus

About the author
Renier is a veterinarian by profession, but apart from his own pets and keeping his animal hospital afloat, he also finds himself busy with creative web design and his websites, motorcycling, photoshopping, micro electronics, non-commercialised music, superhero movies, bad ass seriesses and many other things that is not interesting to most people.
View all posts by Renier Delport