2

I have the following function in my script

import os, re

from pyngrok import ngrok

def server():
        os.system('kill -9 $(pgrep ngrok)')
        ngrok.connect(443, "tcp")
        while True:
                ngrok_tunnels = ngrok.get_tunnels()
                url = ngrok_tunnels[0].public_url
                if re.match("tcp://[0-9]*.tcp.ngrok.io:[0-9]*", url) is not None:
                        print "your url is : " + url
                        break

This is responsible for generating a ngrok tcp link and it works, but it gets stuck like in the image below.

enter image description here

How can I prevent it from being charged? And just print the link, they told me about the monitor_thread mode in False but I don't know how to configure it in my function, thank you very much in advance.

alexdlaird
  • 1,174
  • 12
  • 34
  • The print statement is being print, so it’s unclear what’s you mean by it being “stuck”—if it was stuck we wouldn’t see this output. Is there more code that’s meant to execute after this that isn’t shared? At present, we don’t have enough information to answer the question. – alexdlaird Aug 04 '20 at 22:45
  • With the word stuck I mean that it stays executing the script and it stays that way until I give it control z or control c to stop the process, what I would like is for it to just print the tcp link and send me directly to "root@kali:~/new " – Adrian Guillermo Aug 04 '20 at 22:50

1 Answers1

0

The reason the script is “stuck” is because pyngrok starts ngrok with a thread to monitor logs, and the Python process can’t exit until all threads have been dealt with. You can stop the monitor thread, as shown here in the documentation, or, if you have no use for it, you can prevent it from starting in the first place:


import os, re

from pyngrok import ngrok
from pyngrok.conf import PyngrokConfig

def server():
    os.system('kill -9 $(pgrep ngrok)')
    ngrok_tunnel = ngrok.connect(443, "tcp", pyngrok_config=PyngrokConfig(monitor_thread=False))
    print("your url is : " + ngrok_tunnel.public_url)

However, this still won’t do what you want. If you do this, yes, you will be returned back to the console, but with that the ngrok process will also be stopped, as it is a subprocess of Python at this point. To leave the tunnels open, you need to leave the process running.

alexdlaird
  • 1,174
  • 12
  • 34
  • In my project I run ngrok in the following way: "./ngrok tcp port> /dev/null 2>&1&", so that the execution output doesn't show, but I want to modify that and use its pyngrok module, is there any How can I send the process to /dev/null so it doesn't stop running? and so I get the url grep my localhost:4040/status – Adrian Guillermo Aug 04 '20 at 23:11
  • So it sounds like you have some other non-Python application or script that you’re trying to get to interface with Python (and `pyngrok` specifically) for the purpose of getting `ngrok`’s public URL, right? Since it sounds like your other application isn’t in Python but you have access to Bash commands, why not do something like `ngrok tcp 443 --log stdout` and then `grep` the logs for “tunnel started” and the public URL. – alexdlaird Aug 04 '20 at 23:45
  • Look at this is my previous code: https://ibb.co/WyK58cM And if it works, but I want to stop using the ngrok file and use pyngrok is more practical for me, but I would like it to work similar to this code, for the process to redirect to / dev / null to avoid the output and for the process to continue running How would you do it but using pyngrok? – Adrian Guillermo Aug 05 '20 at 00:08
  • I guess I don't understand your use case enough to know why you'd want to do this, but basically the `server()` method you've defined would be in some file, `server.py`, and you'd call `python server.py &` to have it execute in the background, same as you're doing with the `ngrok` command. You can similarly pipe it's output wherever you want, including `/dev/null`. – alexdlaird Aug 05 '20 at 19:23
  • If indeed, this code is in a.py file and I send it to be called from another script, what happens is that I want the URL to be printed and the user decides whether or not to use the ngrok link, but I still want it to the tcp link is shown, I want to do this but using pyngrok, could you support me in how you would do it? – Adrian Guillermo Aug 05 '20 at 22:02