1

So i'm making a program that runs a flask server an while the flask server runs, connects to localhost.run with ssh. My problem is that if you ssh localhost.run, it does not only return the forwarded url but also a big text with the connection id and other things that i don't need because i only need the url. Here is my code:

import requests, os
from flask import Flask, Response
from multiprocessing import Process
import subprocess

app = Flask(__name__)

# define route for main page
@app.route('/')
def index():
  return Response("<h1>123</h1>")

server = Process(target=app.run)

try:
    server.start()
    print("Starting ssh")
    opt = subprocess.run("ssh -R 80:localhost:5000 nokey@localhost.run")
    print(opt)
except Exception as e:
    print("ERR")
    print(e)

I tried using re.findall, grep but it did not work, it always returned the full text.

  • `os.system` simply returns the exit code, not the output. We can't see from your question what should be filtered out or indeed how you attempted to use `re.findall`. Please [edit] to clarify what the code should do, with representative output from `ssh` and the desired result. – tripleee Apr 27 '23 at 06:10
  • Having said that, the [Stack Overflow `subprocess` tag info page](/tags/subprocess/info) has links to several questions about how to receive output from a subprocess while it's running. – tripleee Apr 27 '23 at 06:12
  • Hm, i now use subprocess.run but it still shows the output if i comment 'print(opt)' out. – appelmoesDEV Apr 27 '23 at 14:17
  • `subprocess.run` alone does not do that; it simply runs a subprocess. You need `capture_output=True` and also probably `text=True`. But `subprocess.run` and the simpler `subprocess.check_output` will not run in parallel with your other Python code; they will wait for the subprocess to finish, and only then provide you with all its output. You really need the bare `Popen` for asynchronous processing, and correspondingly a fair understanding of how to interact with it. Again, the linked subprocess tag info page has links to several FAQs specifically about this. – tripleee Apr 27 '23 at 14:30
  • Now i have this code: `try: server.start() print("Starting ssh") opt = subprocess.run(shlex.split("ssh -R 80:localhost:5000 nokey@localhost.run")) time.sleep(3) #print(opt) except Exception as e: print("ERR") print(e)` but it still show the output of ssh and there is no print. – appelmoesDEV Apr 27 '23 at 15:56
  • Also how to format the code because now it does not look good? – appelmoesDEV Apr 27 '23 at 16:03
  • You can't use code formatting in comments really; your attempt is pretty much as good as it gets. You are still not capturing the output from `subprocess.run`; you want `opt = subprocess.run(["ssh", "-R", "80:localhost:5000", "nokey@localhost.run"], capture_output=True, text=True, check=True)` (or, if you just want to shut it up, `stdout=subprocess.DEVNULL`) – tripleee Apr 28 '23 at 03:31
  • I marked this as a duplicate of a question about how to capture the output from `os.system` since that's what you originally asked about. If you genuinely need to capture output from a subprocess as it's running, it's unclear which of the questions I pointed to earlier should be the duplicate without more details, but you know where to find them. – tripleee Apr 28 '23 at 03:43

0 Answers0