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:
- The server IP address
hostname -I
- The hostname
hostname
- 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?
You Might Also Like
Understanding common network protocols
7 September 2016 |
Using WinSCP to Connect to a Raspberry Pi
1 June 2016 |
How to Share a Raspberry Pi Folder on a Network
18 May 2016 |
Sharing a Raspberry Pi Directory on a Local Area Network
18 April 2016 |
Running Bash Scripts from a Raspberry Pi Home Web Server
24 January 2016 |