43

当我启动一个 ngrok 客户端时,./ngrok tcp 22它在前台运行,我可以看到随机生成的转发 URL,例如tcp://0.tcp.ngrok.io:12345 -> localhost:22.

如果我在后台运行它./ngrok tcp &,我找不到任何查看转发 URL 的方法。如何在后台运行 ngrok 并仍然看到 URL?

4

14 回答 14

52

有几种方法。

您可以:

1)localhost:4040/status在浏览器中访问以查看大量信息,或

2)使用curl打API:localhost:4040/api/tunnels

于 2015-12-23T13:40:40.753 回答
17

这个小 Python (2.7) 脚本将调用 ngrok API 并打印当前 URL:

import json
import os 

os.system("curl  http://localhost:4040/api/tunnels > tunnels.json")

with open('tunnels.json') as data_file:    
    datajson = json.load(data_file)


msg = "ngrok URL's: \n'
for i in datajson['tunnels']:
  msg = msg + i['public_url'] +'\n'

print (msg)
于 2016-01-03T10:43:25.183 回答
14

如果您想获得第一条隧道,那么jq您将成为您的朋友:

curl -s localhost:4040/api/tunnels | jq -r .tunnels[0].public_url

当运行多个 ngrok 实例时,请使用隧道名称/api/tunnels/:name

于 2019-09-09T05:50:05.947 回答
4

如果它对任何人有帮助,我编写了一个快速脚本来提取 Node 中生成的随机 url:

它假设您只对安全 url 感兴趣。

const fetch = require('node-fetch')
fetch('http://localhost:4040/api/tunnels')
  .then(res => res.json())
  .then(json => json.tunnels.find(tunnel => tunnel.proto === 'https'))
  .then(secureTunnel => console.log(secureTunnel.public_url))
  .catch(err => {
    if (err.code === 'ECONNREFUSED') {
      return console.error("Looks like you're not running ngrok.")
    }
    console.error(err)
  })

如果您想要所有隧道:

const fetch = require('node-fetch')
fetch('http://localhost:4040/api/tunnels')
  .then(res => res.json())
  .then(json => json.tunnels.map(tunnel => tunnel.public_url))
  .then(publicUrls => publicUrls.forEach(url => console.log(url)))
  .catch(err => {
    if (err.code === 'ECONNREFUSED') {
      return console.error(
        "Looks like you're not running ngrok."
      )
    }
    console.error(err)
  })
于 2017-10-05T09:56:00.357 回答
3
import json
import requests


def get_ngrok_url():
    url = "http://localhost:4040/api/tunnels/"
    res = requests.get(url)
    res_unicode = res.content.decode("utf-8")
    res_json = json.loads(res_unicode)
    for i in res_json["tunnels"]:
        if i['name'] == 'command_line':
            return i['public_url']
            break

这是 JUN_NETWORKS python 3 代码的编辑。它仅输出 HTTPS URL。我发现 Ngrok 会随机更改 URL 首先显示的顺序,有时会输出 HTTP。附加循环将始终查找名为“command_line”的“隧道”,它是 HTTPS URL。

于 2019-01-08T09:09:24.383 回答
2

运行 ./ngrok http & 这会将 ngrok 隧道作为后台进程运行。Ngrok 通常会打开一个显示分配的 URL 的窗口,但由于我们使用的是 nohup 命令,所以这是不可见的。

因此,然后运行curl http://127.0.0.1:4040/api/tunnels也可以看到 ngrok 分配的 URL

于 2018-01-24T21:38:15.207 回答
2

在 Python3 中

import json
import requests


def get_ngrok_url():
    url = "http://localhost:4040/api/tunnels"
    res = requests.get(url)
    res_unicode = res.content.decode("utf-8")
    res_json = json.loads(res_unicode)
    return res_json["tunnels"][0]["public_url"]

返回的 json 有 2 个用于httphttps的 url 。如果你只想要https url,你res_json["tunnels"][index num]["proto"]

于 2018-11-22T16:13:27.983 回答
2

如果您喜欢 PowerShell,那么它就在变量中。

$ngrokOutput = ConvertFrom-Json (Invoke-WebRequest -Uri http://localhost:4040/api/tunnels).Content
$httpsUrl = $ngrokOutput.tunnels.public_url[0]
$httpUrl = $ngrokOutput.tunnels.public_url[1]
于 2020-06-11T06:44:26.547 回答
1

我检查随机生成的 URL 的最简单方法是转到ngrok 官方站点 > 仪表板 > 端点 > 状态并检查我的端点的 URL 和状态

于 2021-05-04T19:51:54.283 回答
0

在红宝石中

require 'httparty'

# get ngrok public url
begin
  response = HTTParty.get 'http://localhost:4040/api/tunnels'
  json = JSON.parse response.body
  new_sms_url = json['tunnels'].first['public_url']
rescue Errno::ECONNREFUSED
  print 'no ngrok instance found. shutting down'
  exit
end
于 2018-11-28T21:17:30.737 回答
0

Node.js 解决方案。

奖励:它将 url 复制到 Windows、Mac 和 Linux 1中的剪贴板

const http = require("http");
const { execSync } = require("child_process");

const callback = (res) => {
    let data = "";
    res.on("data", (chunk) => (data += chunk));
    res.on("end", () => {
        const resJSON = JSON.parse(data);
        const tunnels = resJSON.tunnels;

        const { public_url: url } = tunnels.find(({ proto }) => proto === "https");

        console.log(url);

        // Copy to clipboard
        switch (process.platform) {
            case "win32":
                execSync(`echo ${url} | clip`);
                break;
            
            case "darwin":
                execSync(`echo ${url} | pbcopy`);
                break;
                
            case "linux":
                // NOTE: this requires xclip to be installed
                execSync(`echo ${url} | xclip -selection clipboard`);
                break;
                
            default:
                break;
        }
    });
};

http.get("http://localhost:4040/api/tunnels", callback);

[1] 你需要先安装xclip

sudo apt-get install xclip
于 2021-01-02T04:08:05.097 回答
0

如果您使用的是 nodejs,我会这样做

const getURL = async () => {
  // inspect if the callback is working at: http://127.0.0.1:4040/inspect/http 
  const ngrok = await import('ngrok')
  const api = ngrok.getApi();
  const { tunnels } = JSON.parse(await api?.get('api/tunnels') ?? '{}')
  // if we already have a tunnel open, disconnect. We're only allowed to have 4
  if (tunnels?.length > 0) await ngrok.disconnect()
  return await ngrok.connect(3000)
}
于 2021-02-17T02:23:20.000 回答
-1

可能是我回答得太晚了,但如果它对任何访问这个问题的人有帮助,我会很高兴。

***以上答案是查看/检查重定向 URL 的解决方案。但是,要在后台运行 ngrok,您可以尝试在 linux 中使用 screen 。如果您需要帮助,请在此处快速参考

步骤: 1. 只需在屏幕中运行 ngrok,然后分离。2.使用上面Gerard给出的python脚本查看URL。

我遵循了相同的过程并且它有效!

于 2017-08-08T22:08:46.110 回答
-1

有一种更好的方法,只需在 ngrok.com 上登录您的帐户即可。您的 URL 将在您的仪表板中。

于 2019-01-18T10:22:26.250 回答