0

I have just started using ngrok, and while using the standard procedure, I can start the tunnel using ./ngrok tcp 22 and see that tunnel open in my dashboard,

But I would like to use pyngrok, and here when I use:

from pyngrok.conf import PyngrokConfig 
from pyngrok import ngrok

ngrok.set_auth_token("<NGROK_AUTH_TOKEN>")

pyngrok_config = PyngrokConfig(config_path="/opt/ngrok/ngrok.yml")

ngrok.get_tunnels(pyngrok_config=pyngrok_config) 
ssh_url  = ngrok.connect()

It connects and generates a tunnel, but I can't see anything open in the dashboard, why?

Maybe because the python script executes and generates URL and then stops and comes out of it, but then how to make it keep running, or how to even start a tunnel using python or even API ? Please suggest the correct script, using python or API?

alexdlaird
  • 1,174
  • 12
  • 34
Nishad Nazar
  • 371
  • 2
  • 3
  • 16
  • I see that this isn't a real auth token, but sometimes it can freak people out that it is, so usually it's recommended you just obfuscate that to something that's obviously fake. In my example below, I simply get the token from an environment variable. – alexdlaird Aug 27 '20 at 15:11
  • 1
    Hi @alexdlaird, I have the proper auth token frm the dashboard, but I didnt post it here due to privacy issues. – Nishad Nazar Aug 27 '20 at 16:13

1 Answers1

2

The thread with the ngrok tunnel will terminate as soon as the Python process terminates. So you are correct, the reason this is happening is because your script is not long lived. The easiest way to accomplish this is by following the example in the documentation.

Another issue is how you're setting the authtoken. Since you're not using the default config_path, you need to set this before setting the authtoken so it gets updated in the correct file (you'd also need to pass it to connect()). There are a couple ways to do this, but the easiest way from the docs is to just update the default config (since that's what will be used if you don't pass a pyngrok_config to any future method calls).

I also see that you're response variable is ssh_url, so you probably want to start a TCP tunnel to a port other than 80 (the default)—perhaps you've configured this in your ngrok.yml, but if not, I've updated the call to connect() to ensure this is the type of tunnel started for you and in case others try to use this same code snippet.

Full disclosure, I am the developer of pyngrok. Here is your code snippet updated with my changes.

import os, time

from pyngrok.conf import PyngrokConfig
from pyngrok import ngrok, conf

conf.get_default().config_path = "/opt/ngrok/ngrok.yml"

ngrok.set_auth_token(os.environ.get("NGROK_AUTH_TOKEN"))

ssh_tunnel = ngrok.connect(22, "tcp")

ngrok_process = ngrok.get_ngrok_process()

try:
    # Block until CTRL-C or some other terminating event
    ngrok_process.proc.wait()
except KeyboardInterrupt:
    print(" Shutting down server.")

    ngrok.kill()
alexdlaird
  • 1,174
  • 12
  • 34
  • Hi @alexdlaird, GLad to get your response, Ill test it now and let you know. I would be so happyy if you stay connected, I would like to know if using pyngrok is the better way or using their own official API to open, stop and get tunnel? – Nishad Nazar Aug 27 '20 at 16:15
  • I have provided auth token, tcp protocol, port 22 in the /opt/ngrok/ngrok.yml file, So do i need to mention the auth token again in the ngrok.set_auth_token ? – Nishad Nazar Aug 27 '20 at 16:18
  • Could you tell me how to stop the tunnel ? (FYI: I'm planning to execute the start py sript remotely, so I can't apply keyboard interrupt, so I need some other way to stop the tunnel. – Nishad Nazar Aug 27 '20 at 16:20
  • You only need to call [`set_auth_token()`](https://pyngrok.readthedocs.io/en/latest/api.html#pyngrok.ngrok.set_auth_token) one time, as that will store it in your config file. Then, so long as you specify that config file, it will be used from then on without needing to set it again. – alexdlaird Aug 27 '20 at 16:36
  • Please familiarize yourself with the `pyngrok` docs [here](https://pyngrok.readthedocs.io/en/latest/index.html), they show you the APIs you can use to interact with `ngrok` in the other ways you've specified. If you want to to interact remotely and in an async fashion, you'll probably want to explore multithreading, and `pyngrok`'s [`api_request()`](https://pyngrok.readthedocs.io/en/latest/api.html#pyngrok.ngrok.api_request) method may be helpful. – alexdlaird Aug 27 '20 at 16:36
  • Sure, I have not used to set_auth_token, because I have already mentioned in the ngrok.yml file, its reading frm there, and it seems it works fine. Thanks to you.. I'm trying to get the Active tunnels, but so far no hope, Im trying, getting this err: pyngrok.exception.PyngrokNgrokError: The ngrok process was unable to start. – Nishad Nazar Aug 27 '20 at 16:41
  • Hi @alexdlaird, Is there any validity for this Auth token ? How to regenerate it, if possible ? – Nishad Nazar Aug 27 '20 at 17:00
  • 1
    This is an `ngrok` question, but I believe you can regenerate it from their UI. – alexdlaird Aug 27 '20 at 17:04
  • Ok, Sure, Why do I always get pyngrok.exception.PyngrokNgrokError: The ngrok process was unable to start., when I try to close the tunnel. – Nishad Nazar Aug 27 '20 at 17:05
  • Any number of reasons. Try [enabling logging](https://pyngrok.readthedocs.io/en/latest/troubleshooting.html), it should give you insight in to why `ngrok` is failing to start. – alexdlaird Aug 27 '20 at 17:07