How to programmatically test if ngrok is already running

Viewed 8015

We have ran ngrok on a localhost PORT say http://localhost:4000. We can manually test if the ngrok is already runing or not by using the following steps:

Check if ngrok is already running:

If the connection happens successfully, the following visual will show up:

ngrok online

If the above visual is not showing, ngrok is not running at all.

  • Under Tunnels section, the following visual will show up:

localhost PORT connected to ngrok online

If the above visual is not showing, ngrok is not running on PORT 4000.

To start ngrok on http://localhost:4000, we need to run ngrok http 4000. After running this command, the above visuals will show up.

Is there some programmatic way to determine if ngrok is already running on the port?

4 Answers

For others stumbling by this question, Make a request to http://localhost:4040/api/tunnels using curl or any request library in the programming language of your choice. It returns a JSON formatted response of the ngrok tunnels and their urls, which you can obtain.. If it does not return anything, it means ngrok is not running

Eg for python:

import requests
import json
from bs4 import BeautifulSoup
req = requests.get('http://127.0.0.1:4040/api/tunnels')
soup = BeautifulSoup(req.text, 'lxml')
tunnelsjson = json.loads(soup.find('p').text)
url = tunnelsjson['tunnels'][0]['public_url']
print(url)

Run netstat -tulnap | grep ngrok

netstat -tulnap shows which processes are running on the machine grep ngrok filters for the ngrok processes.

Related