Python flask 网页版执行shell命令并返回结果['GET', 'POST'] 混合方式

发布时间 2024-01-05 18:04:22作者: 悟透

前言全局说明

Python flask 网页版执行shell命令并返回结果


一、需要安装的库

pip3 install flask -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install subprocess -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

二、index-cmd.html

 <html lang="zh-cn">
    <head>
	    <meta content="text/html; charset=utf-8" http-equiv="content-type" />
	    <style>
		.abc {margin:0 auto;width:720px; solid;text-align:left;}
		</style>
    </head>
    <body>
    	<div class="abc">
			<center><h2>Python-flask 执行 shell 命令</h2></center>
			<br /><br />
			<form id="build_form" method="post" action="/cmdrun">
				<div class="form-group">
					<label for="name">选择:</label>
					<select class="custom-select" name="sele">
						<option selected></option>
						<option value="1">Test 1</option>
						<option value="2">Test 2</option>
						<option value="3">Test 3</option>
						<option value="4">ERR</option>
					</select>
				</div>
				<br />
				<div class="form-group">
					<label for="name"> 填入命令:</label>
					<input type="text" name="command">
					<button type="submit">执行命令</button>
				</div>
				
				<br />
			</form>
			<br />
			{% if result_sele %}
			  <h2>选择:</h2>
			  <p>{{ result_sele }}</p>
			{% endif %}
			{% if result_cmd %}
			  <h2>执行命令:</h2>
			  <p>{{ result_cmd }}</p>
			{% endif %}
			{% if result_output %}
			  <h2>执行过程:</h2>
			  <p>{{ result_output }}</p>
			{% elif result_error %}
			  <h2>执行错误:</h2>
			  <p>{{ result_error }}</p>
			{% endif %}
			<br />
		</div>
    </body>
</html>

页面里的选择项,是为了演示功能。去掉也不影响命令执行


三、index.py 内容

# -*- coding: utf-8 -*-

from flask import Flask
from flask import render_template
from flask import request
import subprocess

app=Flask(__name__)

@app.route("/cmdweb", methods=['GET'])
def cmdindex():
    return render_template('index-cmd.html',**locals())

@app.route("/cmdrun", methods=['POST'])
def cmdruning():
        ## 获取页面选择内容
        sele = request.form['sele']
        if sele == '1':
            sele_value="Test 1"
        elif sele == '2':
            sele_value="Test 2"
        elif sele == '3':
            sele_value="Test 3"
        else:
            return f'Error gbr={sele}'
    
        ## 获取页面要执行的命令
        b_cmd = request.form.get('command')

        ## 命令执行的路径
        cmd_PATH='/tmp/flask_web'

        ## 执行的命令
        # s_cmd = subprocess.check_output(b_cmd, shell=True, cwd=cmd_PATH)
        s_cmd = subprocess.run(b_cmd, shell=True, capture_output=True, text=True, cwd=cmd_PATH)

        ## 处理后的值,返回给页面
        return render_template('index-cmd.html', result_sele=sele_value, result_cmd=b_cmd, result_output=s_cmd.stdout, result_error=s_cmd.stderr)

if __name__ == '__main__':
    # app.debug = True
    # app.run(host='127.0.0.1',port = 5000)
    app.run(host='0.0.0.0',port = 5000)

还可以将:
def cmdindex():和 def cmdruning(): 合并详见下载包里 index_hebin.py 文件写法


四、启动服务

使用python 自带的web服务器

cd py_flask_cmd/
python3 index.py runserver 0.0.0.0:5000
或
python3 index.py runserver

代码里指定 IP和端口,启动时就不用再指定了


五、访问网址

http://127.0.0.1:5000/cmdrun


六、效果

打开网页

image


执行命令成功

image


执行命令失败 (故意写错执行命令)

image


查看设置路径

image



免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源: