美文网首页
用 Flask 实现的在 Web 页面执行 shell 命令的小

用 Flask 实现的在 Web 页面执行 shell 命令的小

作者: awker | 来源:发表于2018-07-13 19:42 被阅读0次

1、项目结构

# tree flask_shell
flask_shell
├── host.cfg                     // ansible inventory文件
├── main.py                     // 项目主要代码
├── requirements.txt            // 项目依赖
└── templates                   // 模板目录
    └── index.html             // web 页面

1 directory, 4 files

2、项目文件内容

# cat flask_shell/host.cfg 
192.168.1.254
192.168.1.125
# cat flask_shell/main.py 
# coding: utf-8
from flask import Flask
from flask import render_template, request
import ansible.runner
import os

app = Flask(__name__)

BASE_DIR = os.path.dirname(os.path.abspath(__file__))


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == "POST":
        cmd = request.form.get('cmd', type=str, default=None)
        ip = request.form.get('ip', type=str, default=None)
        host_cfg = os.path.join(BASE_DIR, 'host.cfg')
        print cmd, ip, host_cfg
        # 1 or None None or 1
        # 1 and None None or 1
        # if cmd or ip:
        # 对比结果
        if cmd and ip:
            runner = ansible.runner.Runner(
                host_list=os.path.join(BASE_DIR, host_cfg),
                module_name='shell',
                module_args=cmd,
                pattern=ip,
                forks=10
            )
            datastructure = runner.run()
            for key, value in datastructure.items():
                if 'contacted' in key:
                    exec_result = value
            return render_template('index.html', result=exec_result, ip=ip, cmd=cmd)
        else:
            return render_template('index.html')
    else:
        return render_template('index.html')


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

# cat flask_shell/requirements.txt 
ansible==1.9.4
Flask==1.0.2
# cat flask_shell/templates/index.html 
<div>
    <div >
        <h1>执行命令</h1>
        <form id="command_form" method="POST" action="/">
            <div>
                <input name="cmd" type="text" placeholder="命令" value="{{ cmd }}">
                <span>@</span>
                <input name="ip" type="text"placeholder="IP" value="{{ ip }}">
                <span>
                    <input name="submit"type="submit" value="执行" />
                </span>
            </div>
        </form>
    </div>
    <div id="result">
        <h3>命令结果</h3>
        <textarea rows="20" cols="100">
            {% if result %}
                {{ result }}
            {% endif %}
        </textarea>
    </div>
</div>

3、使用方法
运行在 Python 2.7.x 环境下,依赖 ansible,flask

# python -V
Python 2.7.5

# git clone https://github.com/ackfin/flash_shell.git

# cd flash_shell

# pip install -r requirements.txt

下面的IP根据你的情况配置,但要配置好ssh免密认证

# cat host.cfg 
192.168.1.254
192.168.1.125

测试 ansible 是否能使用 host.cfg 的 ip 正常运行

# ansible -i host.cfg all -m ping
192.168.1.254 | success >> {
    "changed": false, 
    "ping": "pong"
}

192.168.1.125 | success >> {
    "changed": false, 
    "ping": "pong"
}

启动项目

# python main.py 
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on

比如运行在 192.168.1.254 这台机器上,
就用浏览器打开 http://192.168.1.254:8888


flask_shell_result.png

相关文章

网友评论

      本文标题:用 Flask 实现的在 Web 页面执行 shell 命令的小

      本文链接:https://www.haomeiwen.com/subject/rwuspftx.html