Keep ngrok running forever on Raspberry Pi

Click to enlarge

If you are a Raspberry Pi developer you often need to access web server running on your Raspberry Pi from outside of your home network. For example on your mobile when you are not connected to home Wifi. There are different ways to let the traffic go to your Raspberry Pi from internet such as modify your home router settings and open port forward to your Pi or use HTTP tunnel to route traffic to your Pi. I prefer the later and ngrok is a great utility to achieve that.

But If you are not a paid user of ngrok then your ngrok session expires in 8 hours. You need to restart the ngrok process to obtain new address which is a pain if you want to access your web server from anywhere anytime. It would have been more convenient if you can run ngrok forever without worrying about session expiration every 8 hours.

Here I am going to show you how you can keep your ngrok session running forever.

Please note, this is a hack and probably you should be good if you are prototyping something for your own such as home automation etc. Please consider paid subscription of ngrok if you are thinking to use it in production environment or commercial use.

1. Install Ngrok on Raspberry pi

SSH into your Raspberry Pi or open terminal on your Raspberry pi and execute following commands one by one.

cd /home/pi

sudo wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip

unzip unzip ngrok-stable-linux-arm.zip

sudo chmod +x ./ngrok

./ngrok help

If everything goes fine, you should see ngrok help instruction printed on your terminal.

2. Setup software

Copy both keepalive_ngrok.py and keepalive_ngrok.sh file to /home/pi directory. Grant execution permission on both the files by running following commands on terminal

sudo chmod +x keepalive_ngrok.py
sudo chmod +x keepalive_ngrok.sh

3. Setup cron job

We are going to setup our cron job to execute the shell script every hour at 59 minutes such as 10:59, 11:59.  If you want to change the schedule, please check _run_ngrok method in keepalive_ngrok.py

Execute following command to open crontab editor


crontab -e

Then add below two lines at the end of the file, save and exit.

59 * * * * sh /home/pi/keepalive_ngrok.sh &
@reboot sh /home/pi/keepalive_ngrok.sh &

First line will execute the script at every 59 minutes and the second line to execute the script if your Pi is rebooted.

When the script is executed, it will create HTTP tunnel on the port you specified in the python program. Now it’s up to you what you are going to do with the ngrok address. I usually store it in a dynamoDB table so that I can find the latest address of my Raspberry Pi and connect to it from web. I am sure you will figure out something which works best for you.

I recommend you to have a look at the python code and read all the comments to better understand the changes you may need to do to get it working on your Raspberry Pi

 

This is a proof of concept to keep ngrok running for a long time without paying the monthly subscription. If you get a better hack to achieve the same please do share.

Happy coding !!!

1

Leave a Reply

Your email address will not be published. Required fields are marked *