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:12345 -> localhost:22
.
If I run in it the background with ./ngrok tcp &
, I can't find any way to see the forwarding URL. How can I run ngrok in the background and still see the URL?
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
This little Python (2.7) script will call the ngrok API and print the current URL's:
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)
If it helps anyone I wrote a quick script to extract the generated random url in Node:
It makes assumption you're only interested in the secure 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)
})
If you wanted all tunnels:
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)
})
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.
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
In 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"]
This returned json have 2 url for http and https.
If you want only https url, you res_json["tunnels"][index num]["proto"]
In Ruby
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
There is a better way to do that just login to your account on ngrok.com. Your URL will be in your dashboard.
If you want to get the first tunnel then jq
will be your friend:
curl -s localhost:4040/api/tunnels | jq -r .tunnels[0].public_url
When running more than one instance of ngrok then use the tunnel name /api/tunnels/:name
.
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!
来源:https://stackoverflow.com/questions/34322988/view-random-ngrok-url-when-run-in-background