Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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}:"
@allank

This comment has been minimized.

Copy link

allank commented Jul 27, 2017

I discovered today that ngrok have implemented an API on localhost that returns JSON without having to fiddle about in the HTML of the status page:

curl http://127.0.0.1:4040/api/tunnels

@rjz

This comment has been minimized.

Copy link
Owner Author

rjz commented Sep 19, 2017

Phew! That's much friendlier to work with.

@fvclaus

This comment has been minimized.

Copy link

fvclaus commented Mar 7, 2018

Extract the public url:
curl --silent --show-error http://127.0.0.1:4040/api/tunnels | sed -nE 's/.*public_url":"https:..([^"]*).*/\1/p'

@bpourriahi

This comment has been minimized.

Copy link

bpourriahi commented May 19, 2018

Using jq:
curl --silent http://127.0.0.1:4040/api/tunnels | jq '.tunnels[0].public_url'

@markroxor

This comment has been minimized.

Copy link

markroxor commented Jun 20, 2018

Wish I could give a thumbs up to the comments. Thanks a ton.

@Artistan

This comment has been minimized.

Copy link

Artistan commented Aug 7, 2018

thanks for the inspiration @rjz and @bpourriahi

I created a couple scripts that will start ngrok in the background, get the new domain name, and update my domain name on digitalocean so that i have a consistent cname to use in development.

https://gist.github.com/Artistan/e1da8a060900d5db3c3eb001e076b4e9

CHEERS!

@jhalehol

This comment has been minimized.

Copy link

jhalehol commented Aug 21, 2018

Thanks @fvclaus, that's works for me

@derianpt

This comment has been minimized.

Copy link

derianpt commented Sep 27, 2018

Thanks @fvclaus!

@ellisgl

This comment has been minimized.

Copy link

ellisgl commented Oct 18, 2018

Thanks @fvclaus!

@adamshamsudeen

This comment has been minimized.

Copy link

adamshamsudeen commented Nov 22, 2018

Thanks @fvclaus :)

@ChieftainY2k

This comment has been minimized.

Copy link

ChieftainY2k commented Jan 24, 2019

Thanks guys! 👍

@krzyk

This comment has been minimized.

Copy link

krzyk commented Apr 21, 2019

my ngrok doesn't expose anything on port 4040, Is there a specific option when starting ngrok that I should use (normally I start it using ngrok tcp 123)

@scippacercola

This comment has been minimized.

Copy link

scippacercola commented May 20, 2019

Thanks. On minimal system setup I used:
echo -e 'GET /api/tunnels HTTP/1.1\nHost: 127.0.0.1\n' | nc localhost 4040

@stvnmntjy

This comment has been minimized.

Copy link

stvnmntjy commented Jan 23, 2020

Apologies for the rez, but to answer @krzyk: You can configure a web_addr property in the ngrok.yml (see "The ngrok configuration file" and "web_addr" here: https://ngrok.com/docs).

If you just want to know the port, it's the one listed in ngrok's session under Web Interface:

ngrok

Mine alternates from 4040 to 4041 at-whim.

@swathi-ashok

This comment has been minimized.

Copy link

swathi-ashok commented Feb 7, 2020

Thanks a lot. Works like charm. This is exactly what I was looking for.

@certainty157502

This comment has been minimized.

Copy link

certainty157502 commented Mar 17, 2020

Extract the public url:
curl --silent --show-error http://127.0.0.1:4040/api/tunnels | sed -nE 's/.*public_url":"https:..([^"]*).*/\1/p'

Hi, I have two tunnels in Ngrok.. how do I extract the public url of two different tunnels?

@swathi-ashok

This comment has been minimized.

Copy link

swathi-ashok commented Mar 18, 2020

curl --silent http://127.0.0.1:4040/api/tunnels | jq '.tunnels[0].public_url' ---> This will give you the public URL of the first tunnel
curl --silent http://127.0.0.1:4041/api/tunnels | jq '.tunnels[0].public_url' ---> This will give you the public URL of the second tunnel.
Likewise, if you have multiple tunnels, increment the port number (4041) in the above URL by one to get the corresponding public URL.

Ex : 4042 for third tunnel, 4043 for fourth tunnel and so on.

@shamiul94

This comment has been minimized.

Copy link

shamiul94 commented May 19, 2020

This one worked for me.

curl -s localhost:4040/api/tunnels | jq -r .tunnels[0].public_url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.