I'm trying to make a python script wich reports me the port on 0.tcp.ngrok.io is started when I run the code on terminal (after moving ngrok executable file to /usr/local/bin)

ngrok tcp 22

I get ths kind of output

ngrok by @inconshreveable                                       (Ctrl+C to quit)

Session Status                connecting                                        
Version                       2.2.4                                             
Region                        United States (us)                                
Web Interface                                    
Forwarding                    tcp://0.tcp.ngrok.io:13014 -> localhost:22                                                                                 
Connections                   ttl     opn     rt1     rt5     p50     p90       
                              0       0       0.00    0.00    0.00    0.00  

My first attempt is to log the subprocess stdout to a variable , but as the stdout is cyclic the stdout.read() never ends this is the code

import subprocess

ngrok = subprocess.Popen(['ngrok','tcp','22'],stdout = subprocess.PIPE)

output_text = ngrok.stdout.read() # script stops here forever

[**code for getting domain:port from output_text**]

how can I get a "snapshot" of stdout to a variable , without stoping ngrok?

Is there another way of doing this (next try would be a webscrapper on localhost , but it would be nice to have this knowledge for other commands , such as "top")

thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.