View random ngrok URL when run in background

前端 未结 12 1706
旧时难觅i
旧时难觅i 2021-02-02 08:16

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:

相关标签:
12条回答
  • 2021-02-02 08:33

    A Node.js solution.

    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
    
    0 讨论(0)
  • 2021-02-02 08:36
    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
    

    This is an edit of JUN_NETWORKS python 3 code. It outputs the HTTPS URL only. I find Ngrok will randomly change the order of which is URL is displayed first sometimes outputting HTTP. The additional loop will consistently look for the 'tunnel' named 'command_line' which is the HTTPS URL.

    0 讨论(0)
  • 2021-02-02 08:37

    There are a couple of ways.

    You can either:

    1) Visit localhost:4040/status in your browser to see a bunch of information, or

    2) Use curl to hit the API: localhost:4040/api/tunnels

    0 讨论(0)
  • 2021-02-02 08:41

    If you love PowerShell, here it is in variables.

    $ngrokOutput = ConvertFrom-Json (Invoke-WebRequest -Uri http://localhost:4040/api/tunnels).Content
    $httpsUrl = $ngrokOutput.tunnels.public_url[0]
    $httpUrl = $ngrokOutput.tunnels.public_url[1]
    
    0 讨论(0)
  • 2021-02-02 08:46

    Run ./ngrok http & This runs the ngrok tunnel as a background process. Ngrok usually opens a window showing the assigned URL but since we are using the nohup command this is not visible.

    Thus, then run curl http://127.0.0.1:4040/api/tunnels too see the URL assigned by ngrok

    0 讨论(0)
  • 2021-02-02 08:46

    May be I'm a little too late in answering but would be glad if it is helpful for anyone visiting the question.

    ***Above answers are solutions to see/check the redirection URL. However to run ngrok in background, you could try using screen in linux . Incase you need help here a quick reference

    Steps: 1. Just run the ngrok in screen and then detach. 2. Use the python script given by Gerard above to see the URL.

    I have followed the same process and it works!

    0 讨论(0)
提交回复
热议问题