#!/usr/bin/env python # -*- coding: utf-8 -*- # @File : core.py # @Author: becivells #@Contact : becivells@gmail.com # @Date : 2017/10/19 #@Software : PyCharm # @Desc : import re import tempfile import subprocess from config import repo from config import EXEC_WHITE_LIST from config import PATH_WHITE_LIST def runcmd(cmd,path): out_temp = tempfile.SpooledTemporaryFile(max_size=10 * 1000) try: fileno = out_temp.fileno() p = subprocess.Popen(cmd, shell=True,cwd=path, stdout=fileno, stderr=fileno) p.wait() out_temp.seek(0) return out_temp.read() except Exception as e: return str(e) finally: if out_temp: out_temp.close() def allow_exec(cmd): ''' 白名单函数允许指定命令运行并且限制长度 :param cmd: :return: ''' cmd = ' '.join(cmd.split()) #移除多余空格 if len(cmd) > 100: #限制长度 return False for whitecmd in EXEC_WHITE_LIST: result = re.findall(whitecmd,cmd) if result and cmd == result[0]: return True return False def allow_path(path): ''' 限制路径 :param path: :return: ''' path = path.strip() for whitepath in PATH_WHITE_LIST: result = re.findall(whitepath,path) if result and path == result[0]: return True return False def getrepo(): ''' 更新配置文件 :return: ''' return repo