最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to programmatically test if ngrok is already running - Stack Overflow

programmeradmin0浏览0评论

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:

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

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

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?

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:

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

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

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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 2, 2017 at 6:07 XameerXameer 31.2k27 gold badges146 silver badges229 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

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 this command to see using ports:

sudo lsof -PiTCP -sTCP:LISTEN

You can check it by python:

import socket;
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1',4000))
print(result)
if result == 0:
    print "Port is not open"
else:
    print "Port is open"

P.S. if port is in use, result is 0, if not it is 61.

Run netstat -tulnap | grep ngrok

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

发布评论

评论列表(0)

  1. 暂无评论