Skip to content

Instantly share code, notes, and snippets.

@danuw
Last active January 19, 2021 19:12
Show Gist options
  • Save danuw/9879357e93bbe8411f7ab2f9d2974a62 to your computer and use it in GitHub Desktop.
Save danuw/9879357e93bbe8411f7ab2f9d2974a62 to your computer and use it in GitHub Desktop.
use ngrok to connect via ssh to remote machine et get notified of new address via IFTTT
#!/bin/sh
## based on post by bobmarksie at https://stackoverflow.com/questions/27162552/ngrok-running-in-background
# Set local port defaulted to 22
LOCAL_PORT=22
IFTTT_KEY="<key goes here>" # make sure to update with key from your webhook in IFTTT
echo "Start ngrok in background on port $LOCAL_PORT"
nohup ./ngrok tcp ${LOCAL_PORT} &>/dev/null &
echo -n "Extracting ngrok public url ."
NGROK_PUBLIC_URL=""
while [ -z "$NGROK_PUBLIC_URL" ]; do
# Run 'curl' against ngrok API and extract public (using 'sed' command)
export NGROK_PUBLIC_URL=$(curl --silent --max-time 10 --connect-timeout 5 \
--show-error http://127.0.0.1:4040/api/tunnels | \
sed -nE 's/.*public_url":"tcp:..([^"]*).*/\1/p')
sleep 1
echo -n "."
done
echo "Saving to nglog.json"
curl http://127.0.0.1:4040/api/tunnels > nglog.json
echo
echo "NGROK_PUBLIC_URL => [ $NGROK_PUBLIC_URL ]"
curl -d '{"value1":"tcp://'"$NGROK_PUBLIC_URL"'", "value2":"<device name to easily recognize>"}' -H "Content-Type: application/json" -X POST "https://maker.ifttt.com/trigger/notification/with/key/$IFTTT_KEY"
#!/bin/sh
## based on post by bobmarksie at https://stackoverflow.com/questions/27162552/ngrok-running-in-background
echo "Stopping background ngrok process"
kill -9 $(ps -ef | grep 'ngrok' | grep -v 'grep' | awk '{print $2}')
echo "ngrok stopped"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment