ngrok获取分配的子域名

10
我有一个NodeJS脚本,可以启动ngrok实例并启动ngrok二进制文件。
但是我需要能够返回自动生成的URL,我无法在文档中找到如何实现此功能的任何信息。
例如,当您运行 “ngrok http 80” 时,它会启动并为您生成一个随机唯一的URL。

enter image description here


你的意思是想要获取自动生成的URL吗? - abdulbarik
@abdulbarik 我在问题中加入了一些更多的信息 :) - owenmelbz
2个回答

6

这个问题有点老了,不过我想提供另一种更通用的选择,因为它不需要NodeJS。

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

这个任务仅仅是检查调用 api/tunnels 的响应,通过对结果文本进行文本处理 (sed) 来识别公共URL。


5

ngrok 会在 http://localhost:4040/api/tunnels 提供隧道信息。

使用 curl 和 jq

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

=> https://719c933a.ap.ngrok.io

使用curl和ruby

curl -Ss http://localhost:4040/api/tunnels | \
  ruby -e 'require "json"; puts JSON.parse(STDIN.read).dig("tunnels", 0, "public_url")'

=> https://719c933a.ap.ngrok.io

使用curl和node

json=$(curl -Ss http://127.0.0.1:4040/api/tunnels);
node -pe "var data = $json; data.tunnels[0].public_url"

=> https://719c933a.ap.ngrok.io

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接