Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically test if ngrok is already running

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:

  • Hit http://127.0.0.1:4040/status

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?

like image 484
xameeramir Avatar asked Aug 02 '17 06:08

xameeramir


People also ask

How do I close an active session in Ngrok?

Use Ctrl+C to stop ngrok and sharing.

Can Ngrok run forever?

One of the new limits is a session expiration time of 2 hours. When an Ngrok connection reaches this length it stops working, and the only way to restore it is by stopping and restarting the ngrok command, which causes a new randomly generated URL to be used.

How do you authenticate Ngrok?

The ngrok agent connects to the ngrok cloud and authenticates using the authtoken that is created when you sign up for ngrok. You can find the authtoken in the ngrok Dashboard. This command will create a configuration file at the default location for your system.


1 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)
like image 186
Sachin Ramanathan Avatar answered Sep 21 '22 09:09

Sachin Ramanathan