当我使用./ngrok tcp 22
启动ngrok客户端时,它会在前台运行,并且我可以看到随机生成的转发URL,例如tcp://0.tcp.ngrok.io:12345 -> localhost:22
。
如果我使用./ngrok tcp &
在后台运行,我找不到任何方式来查看转发URL。如何在后台运行ngrok并仍然能够查看URL?
当我使用./ngrok tcp 22
启动ngrok客户端时,它会在前台运行,并且我可以看到随机生成的转发URL,例如tcp://0.tcp.ngrok.io:12345 -> localhost:22
。
如果我使用./ngrok tcp &
在后台运行,我找不到任何方式来查看转发URL。如何在后台运行ngrok并仍然能够查看URL?
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
额外福利:它可以在 Windows、Mac 和 Linux 上复制 url1到剪贴板。
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
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)
}
localhost:4040/api
提供的ngrok Agent API中获取第一个URL。//fetch xml from API `api/tunnels/command_line`
using var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get,
"http://localhost:4040/api/tunnels/command_line");
request.Headers.Add(HttpRequestHeader.Accept.ToString(), "application/xml");
var response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
//parse and get the first PublicURL element's text
const string Xpath = "/tunnelResource/PublicURL";
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(content);
var xmlNode = xmlDocument.SelectSingleNode(Xpath);
var xmlElement = xmlNode as XmlElement;
var url = xmlElement.InnerText;
可能我回答有点晚,但如果对访问该问题的任何人有帮助,我会很高兴。
上面的答案是查看/检查重定向网址的解决方案。然而,要在后台运行ngrok,您可以尝试在Linux中使用屏幕。如果需要帮助,请参考此处的参考链接。
步骤: 1.只需在屏幕上运行ngrok,然后分离。 2.使用Gerard上面提供的Python脚本查看URL。
我已经按照同样的过程操作,并且它有效!
有更好的方法可以做到这一点,只需登录ngrok.com上的帐户即可。您的URL将显示在您的仪表板中。