Remote Access

@TrystanLea

This post might be better titled … Remote Access Update.

I assume you have a continued interest in solutions that would do an Export Backup on a remote RPi/emonTx and then download it to a local RPi …

Using remote.it and the following script, it is possible to do just that and the script can run in the background as a cronjob not needing to enter user name or password.

The script is an adaptation & extension of the script p2p.zip referenced in the remote.it Guide.

The first requirement is to sign up for a remote.it account. This is free and one user with up to 10 devices gets free use under their Fair Use Policy.

Their Guide is comprehensive covering Windows, Mac, Linux and RPi which makes it a bit difficult to follow. Their Support is super responsive.

The first step is to install connectd on each RPi and then using an interactive menu to define each Device (RPi) and the Services you require on each Device. The script below requires the ssh service but do also install the web service (you can then access the emoncms Web Interface on the remote RPi ).

If REMOTE RPi A is at a remote geographical location then you’ll need to make a ‘service’ visit to perform the above steps. But once done, you’ll have remote access to RPi A and the rest of the setup can be done back at ‘local base’ – in my case from a Windows laptop with an ssh terminal for each RPi open on the desktop.

If just experimenting, RPi A can be on your local network but you access it as if it were remote via the remote.it server.

The script requires 3 bits of data … your remote.it username & password and the uid. This is the UID of RPi B’s ssh service (ref the remote.it Guide)

Finally, in order to avoid entering user names and passwords, do the following on RPi A … ssh-keygen and just press ENTER when asked for a passphrase and then accept the default location which is … /home/pi/.ssh/id_rsa

Then carefully copy & paste /home/pi/.ssh/id_rsa.pub from RPi A to RPi B /home/pi/.ssh/authorized_keys

You can now run the script as and when needed on RPi A (after making it executable) or incorporate it into a cronjob.

In summary – the script opens a remote connection, securely (scp) copies an emoncms-backup export file with today’s date and then closes the remote connection. In my case, a 150MB export file takes 6 mins to copy.


#!/bin/bash

## Purpose:

## To use remote.it to create a Peer to Peer connection from a remote RPi A to a local RPi B

## Both RPi's are running emoncms but RPi B has no emonTx/sensors connected

## To cause RPi A to run an EXPORT data backup (using the OEM supplied script) and then to copy this to RPi B

## The task to be completed without the need to interactively input user names or passwords

## Hence - this script can also be run as a cronjob on RPi A

## This script is an adaptation & extension of the script p2p.zip referenced in the remote.it Guide

## Pre-requisites:

## id_rsa keys must be generated on RPi A WITHOUT A PASSWORD and in the default location

## /home/pi/id_rsa.pub for RPi A must be copied to RPi B /home/pi/.ssh/authorized_keys

## On both RPi A and RPi B, permissions need to be set as follows ...

## For /home/pi/.ssh 744 and for /home/pi/.ssh/authorized_keys 644

## Some remote.it actions are also required ...

## connectd installed on both RPi A and RPI B

## -ssh Services created on both RPi A and RPi B

## Now begin ...

## Run the OEM emoncms export script

cd /opt/emoncms/modules/backup

./emoncms-export.sh

## Continue ...

sleep 5

## DEFINE remote.it PARAMETERS ...

username=~~~~~~~~~~~~

password=~~~~~~~~~~~~

uid=~~~~~~~~~~~~~~~~ ## This is the UID of RPi B's ssh service (ref the remote.it Guide)

address=127.0.0.1

port=33300

## Check connectd installation ...

if [ "$(which connectd)" = "" ]; then

echo "connectd is not properly installed."

exit 1

fi

## Set up P2P connection ...

echo

echo "Setting up a P2P connection to $uid on $address port $port."

echo

b64username="$(echo "$username" | tr -d '\n' | base64)"

b64password="$(echo "$password" | tr -d '\n' | base64)"

connectd -c $b64username $b64password "$uid" T"$port" 2 "$address" 12 &

## Get the process ID once the connectd command has completed

pid=$(ps ax | grep $b64username | grep $uid | awk '{ print $1 }')

if [ "$pid" != "" ]; then

sleep 10

echo

echo "Connection to $uid is now active on $address port $port."

echo

echo watchman SUCCESSFULLY CONNECTED to watchman-51

echo

fi

## Copy export backup file to RPi B

date=$(date +"%Y-%m-%d")

scp -v -P 33300 /var/opt/emoncms/backup/emoncms-backup-$date.tar.gz [email protected]:/home/pi

## rsync is an alternative to scp but it is no faster for a single large file

### rsync -v -e 'ssh -p 33300' --progress /var/opt/emoncms/backup/emoncms-backup-$date.tar.gz [email protected]:/home/pi

## This alternative scp command may also work?

### scp -v -i /home/pi/.ssh/id_rsa -P 33300 /var/opt/emoncms/backup/emoncms-backup-$date.tar.gz [email protected].$

if [ $? -ne 0 ]

then

echo watchman FAILED TO EXPORT DATA to watchman-51

else

echo

echo watchman SUCCESSFULLY EXPORTED DATA to watchman-51

fi

## Close P2P connection ...

kill $pid

echo

echo connectd pid $pid has been killed

echo

echo Connection to remote.it and the Peer to Peer connection is now CLOSED

exit

I now have this script successfully running as a cronjob on RPi A.

And having recently suffered a big data loss because of a power cut at the remote location, I now feel more secure – risk of power cuts, fire, flood, burglary, etc at the remote location is now covered.

Admittedly, a new data security risk exists for the approx 6 mins that the peer to peer connection exists between RPi A and RPi B but I’m not qualified to quantify that.

Hope this is of interest