Skip to content

Instantly share code, notes, and snippets.

@rjz
Created August 9, 2016 16:20
Show Gist options
  • Save rjz/af40158c529d7c407420fc0de490758b to your computer and use it in GitHub Desktop.
Save rjz/af40158c529d7c407420fc0de490758b to your computer and use it in GitHub Desktop.
Get ngrok hostname from command line
#!/bin/sh
# ngrok's web interface is HTML, but configuration is bootstrapped as a JSON
# string. We can hack out the forwarded hostname by extracting the next
# `*.ngrok.io` string from the JSON
#
# Brittle as all get out--YMMV. If you're still reading, usage is:
#
# $ ./ngrok_hostname.sh <proto> <addr>
#
# To retrieve the ngrok'd URL of an HTTP service running locally on :3332, use:
#
# $ ./ngrok_hostname.sh http localhost:3332
#
# The protocol (http, https, etc) of the forwarded service
PROTO=$1
# The address of the forwarded service
ADDR=$2
# Hack JSON out of the web interface bootstrap
json=$(curl -s localhost:4040/inspect/http \
| grep -oP 'window.common[^;]+' \
| sed 's/^[^\(]*("//' \
| sed 's/")\s*$//' \
| sed 's/\\"/"/g')
# Parse JSON for the URLs matching the configured `$ADDR`
hosts=$(echo $json \
| jq -r ".Session.Tunnels \
| values \
| map(select(.Config.addr == \"$ADDR\") | .URL) | .[]")
echo "$hosts" | grep "^${PROTO}:"
@funder7
Copy link

funder7 commented Apr 11, 2021

should be latest one, just tried one hour ago

Just tried with the latest stable, it is working as usual.
Did you add inspect: true in your config file? (ngrok.yml)

@reachlin
Copy link

no luck.thx anyway

@robin-weaver
Copy link

url_new_https = response.json()["tunnels"][0]["public_url"]

Nice, good stuff. Amazing thread here.

@ecarrera
Copy link

ecarrera commented Feb 3, 2024

Shell script

  • OSX Sonoma 14.3
  • ngrok version 3.5.0
local json_data=$(curl -s localhost:4040/api/tunnels)
local public_url=$(echo "$json_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])")
echo $public_url

@chikitai
Copy link

local json_data=$(curl -s localhost:4040/api/tunnels)
local public_url=$(echo "$json_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])")
echo $public_

@chikitai
Copy link

local json_data=$(curl -Chikita Isaac bitcoin.comlocalhost:4040/api/tunnels)
local public_url=$(echo "$json_data" | python3 -c "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])")varonis.com
echo $public_freewallet(100000000000000000000000000000000000000000000000000009)

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