Skip to content

Instantly share code, notes, and snippets.

@realyukii
Last active September 16, 2024 05:02
Show Gist options
  • Save realyukii/1e0ad1fc07c41b26b0d54986f248ff09 to your computer and use it in GitHub Desktop.
Save realyukii/1e0ad1fc07c41b26b0d54986f248ff09 to your computer and use it in GitHub Desktop.
run ngrok in background

start the program in background

setsid ngrok tcp <port> &>/dev/null </dev/null

optional, if you want store the log, enable ngrok option --log=stdout

parse the json string with node

node -pe 'JSON.parse(process.argv[1])' $(curl -s http://localhost:4040/api/tunnels)
# atau
curl -s http://localhost:4040/api/tunnels | node -pe 'JSON.parse(require("fs").readFileSync("/dev/stdin").toString())'
# atau
curl -s http://localhost:4040/api/tunnels | xargs -0 -I {} node -pe 'JSON.parse(JSON.stringify({}))'
# atau
curl -s http://localhost:4040/api/tunnels | xargs -0 -I {} node -pe 'JSON.parse(process.argv[1])' {}

an alternative: ngrok api endpoints list --api-key $(cat .ngrok_api_key)

setup tunnel to start multiple service

example ngrok config (ngrok config check):

version: "2"
authtoken: <redacted>
tunnels:
  ssh:
    addr: 22
    proto: tcp
  http:
    addr: 80
    proto: http

start multiple service with single tunnel:

ngrok start ssh http
#or
ngrok start --all

reference

@realyukii
Copy link
Author

get the ngrok configuration:

ngrok config check | awk '{print $NF}'
# or with grep
ngrok config check | grep -Eo '/[^ ]+$'
# or with cut
ngrok config check | cut -d ' ' -f 5-
# or with sed
ngrok config check | sed -n 's/Valid configuration file at //p'

@realyukii
Copy link
Author

you can fetch information from rest api provided by ngrok: ngrok api tunnel-sessions list --api-key=<your-api-key>

@realyukii
Copy link
Author

ngrok api docs

curl \
-X GET \
-H "Authorization: Bearer {API_KEY}" \
-H "Ngrok-Version: 2" \
https://api.ngrok.com/endpoints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment