How To Raspberry Pi

Use Serveo.net to access your Raspberry Pi from internet

A Raspberry Pi

Supercharging your SSH by automating connection and startup

Now that your website is ready, you will need to run the SSH command every couple of hours and also each time Raspberry Pi restarts. To fix this we are going to:

a) Build a script using autossh
b) Start the script on startup

a) Build a script using autossh

We first install autossh from the Raspberry Pi repository by running the command.

sudo aptitude install autossh -y

Next We create a file that we will use to enter the scripts to run at startup, type

nano mysite.sh

Then add this line to the file.

#!/bin/bash
function localtunnel {
autossh -i /home/pi/mysiteKeyPair -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -R www.mysite.com:80:localhost:80 -R mysite.com:80:localhost:80 serveo.net
}
until localtunnel; do
echo "local tunnel crashed"
sleep 2
done

Then save and exit.

In the command above we are requesting two tunnels, one to https://www.mysite.com, the other to https://mysite.com.

For more details you can always read this guide about ssh tunneling.

Now we need to make out script an executable, so we run the following

sudo chmod +x mysite.sh
Statup script owner and permissions
Making the script executable
b) Start the script on startup

There are several ways to do this, but what works for me is to see the commands on the terminal. To do that we first edit the autostart file.

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

Insert this line at the bottom, then save and exit.

@lxterminal -e /home/pi/mysite.sh
LXDE Autostart to start a terminal with script running
LXDE Autostart

Next time you restart your raspberry pi the site will still be accessible.

That’s it. Let us know your thoughts / issues in the comments below.
ALSO READ  [VIDEO] How To: Microsoft Sticky Notes Cloud Sync

Leave a Reply

avatar
  Subscribe  
Notify of