Skip to content

Instantly share code, notes, and snippets.

@theely
Last active August 1, 2022 17:42
Show Gist options
  • Save theely/2fc3173ab8470e42ca6e64e98935d021 to your computer and use it in GitHub Desktop.
Save theely/2fc3173ab8470e42ca6e64e98935d021 to your computer and use it in GitHub Desktop.
ngrok service for Raspberry Pi
1) set-up ngrok
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null && echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list && sudo apt update && sudo apt install ngrok
ngrok config add-authtoken <your token here>
2) install ssmtp
sudo apt install ssmtp
3) crate /etc/init.d/ngrok
4) crate /home/pi/.config/ngrok/ngrok.yml
5) create a gmail app password to be used in the sendNgrokAddress.py file
6) crate /home/pi/sendNgrokAddress.py
7) set-up ngrok service
sudo chmod +x /etc/init.d/ngrok
sudo sudo update-rc.d ngrok defaults
#FILE PATH: /etc/init.d/ngrok
#!/bin/sh
### BEGIN INIT INFO
# Provides: ngrok
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the ngrok tunnel
# Description: starts ngrok using start-stop-daemon
### END INIT INFO
case "$1" in
start)
echo "Ngrok service is starting"
ngrok start --all --config=/home/pi/.config/ngrok/ngrok.yml --log=stdout > /home/pi/ngrok.log &
python /home/pi/sendNgrokAddress.py
echo "Ngrok service was started"
;;
stop)
echo "Ngrok is stopping"
killall ngrok
echo "Service ngrok was stopped"
;;
*)
echo "Usage: /etc/init.d/ngrok.sh {start|stop}"
exit 1
;;
esac
exit 0
#FILE PATH: /home/pi/.config/ngrok/ngrok.yml <- delete this line
version: "2"
authtoken: <your token here>
web:
proto: http
addr: 80
#FILE PATH: /home/pi/sendNgrokAddress.py <- delete this line
import smtplib
import re
import time
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = '<your user here>' #change this to match your gmail account
GMAIL_PASSWORD = '<your password here>' #change this to match your gmail app-password
class Emailer:
def sendmail(self, recipient, subject, content):
#Create Headers
headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
"MIME-Version: 1.0", "Content-Type: text/html"]
headers = "\r\n".join(headers)
#Connect to Gmail Server
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
#Login to Gmail
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
#Send Email & Exit
session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
session.quit
sender = Emailer()
sendTo = '<your email here>'
emailSubject = "Printer Tunnel"
emailContent = "Tunnel address "
time.sleep(30) #wait till Ngrok starts
with open("/home/pi/ngrok.log", "r") as fileInput:
listLines = fileInput.readlines()
for line in listLines:
x = re.search(".*url=(https.*)", line)
try:
emailContent += x.group(1)
print emailContent
except:
continue
#Sends an email to the "sendTo" address with the specified "emailSubject" as the subject and "emailContent" as the email content.
sender.sendmail(sendTo, emailSubject, emailContent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment