1、Windows server 通過python的flask執行命令
from flask import Flask, request, abort
import subprocess
from flask_basicauth import BasicAuth
app = Flask(__name__)
# 獲取url是進行賬號密碼認證,設置url的賬號密碼
app.config['BASIC_AUTH_USERNAME'] = '賬號自設定'
app.config['BASIC_AUTH_PASSWORD'] = '密碼自設定'
app.config['BASIC_AUTH_FORCE'] = True # 整個站點都驗證
# BasicAuth初始化
basic_auth = BasicAuth(app)
# 允許訪問的IP地址列表
allowed_ips = ['10.1.1.2', '10.1.1.1', '127.0.0.1', 'localhost']
# 使用 before_request 鉤子進行 IP 地址檢查
@app.before_request
def limit_remote_addr():
if request.remote_addr not in allowed_ips:
abort(403)
@app.route('/dhcp/showall', methods=['GET'])
def showall():
return subprocess.check_output('netsh dhcp server show all ', shell=True, text=True)
@app.route('/ipconfig', methods=['GET'])
def ipconfig():
return subprocess.check_output('ipconfig', shell=True, text=True)
# 設置 host 為 0.0.0.0,以便監聽所有網絡接口
app.run(host='0.0.0.0', port=8080, debug=True)