When I start an ngrok client with ./ngrok tcp 22
it runs in the foreground and I can see the randoming generated forwarding URL, such as tcp://0.tcp.ngrok.io:
Bonus: It copies the url to the clipboard in Windows, Mac and Linux1
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] You need to install xclip
first:
sudo apt-get install xclip