I'm currently programming a Web app to Remotely Manage my Raspberry Pi. I already have some features, but I can not figure out how to make a Console. This is what I currently have:
from flask import * ##I know that this is not the “right way” but it is still in development
from subprocess import PIPE, run
app = Flask(__name__)
password = 'MYPASSWORD'
@app.route('/')
def console():
if request.method == "POST":
pw = request.form["pw"] #short for password
global password
if pw == password:
cmd = request.form["cmd"]
commandoutput = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
return render_template('console.html', commandoutput=str(commandoutput.stdout).replace('\n', '\n'))
else:
return 'Auth Error'
else:
return render_template('console.html')
Here is the console.html file (the important part only):
<p>ADMIN PANAL</p>
<body>
latest output:
<p3>{{commandoutput}}</p3>
<body>
<form action="#" method="post">
<p>Command:</p>
<p><input type="text" name="cmd" /></p>
<p><input type="password" name="pw">
<p><input type="submit" value="submit"/></p>
</form>
This Code works fine unless the user wants to use a command like sudo, nano or python(console) or any command that has to be stopped manually or needs any kind of user input.
What would be an easy way to solve this problem.
CodePudding user response:
What are trying to achieve is not trivial and can be a huge security issue for the network the raspberry pi is running on.
Be sure that you rasp is not expose to the internet, only accessible locally on a safe network.
Then you can use something like https://github.com/WalkerWang731/WebTerminal which should save you the work of doing it yourself, or give you a source of inspiration on how to achieve your goal ( which I repeat is not trivial).