File size: 3,173 Bytes
992ffd2 2462928 5265919 b0e524c 201ec15 2462928 201ec15 2462928 8330f0b 2462928 201ec15 2462928 497f5c4 2462928 8330f0b 2462928 8330f0b 2462928 8330f0b 553eb04 b0e524c ce09e6a 2462928 b0e524c 2462928 201ec15 cf60666 2462928 497f5c4 2462928 cf60666 2462928 497f5c4 2462928 22037ce 2462928 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import subprocess
import sys
import logging
import os
import binascii # 用于将文本转换为十六进制
import re # 用于正则表达式
StartBL = ""
# 打开并执行txt文件
with open('/etc/main/StartBianLiang.txt', 'r') as file:
code = file.read()
# 去除每行开头和结尾的空格、空行等
cleaned_code = '\n'.join(line.strip() for line in code.splitlines() if line.strip())
# 执行干净的代码
exec(cleaned_code)
# 获取元组中的字符串并去除括号
StartBL = StartBL[0].strip('()')
# 检查是否存在 logs_run.txt 文件,如果存在则删除它
log_file = '/etc/main/logs_run.txt'
if os.path.exists(log_file):
os.remove(log_file)
# 检查是否存在 url.txt 文件,如果存在则删除它
url_file = '/etc/main/url.txt'
if os.path.exists(url_file):
os.remove(url_file)
# 配置日志
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 创建一个将输出同时发送到控制台和日志文件的处理程序
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logging.getLogger().addHandler(console_handler)
# 重定向标准输出和标准错误到日志文件和控制台
sys.stdout = logging.getLogger().handlers[0].stream
sys.stderr = logging.getLogger().handlers[0].stream
# 更改当前工作目录为 /etc/main
os.chdir('/etc/main/')
try:
# 在 try 块中运行 /etc/main/webui.py,并将输出重定向到日志文件
with open(log_file, 'a') as log_file_handle, open(url_file, 'a') as url_file_handle:
process = subprocess.Popen(['python', StartBL ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
url_pattern = re.compile(r'https?://\S+')
for line in process.stdout:
if "Public" in line or "Application" in line:
# 将包含 "Public" 或 "Application" 的行转换为十六进制并写入 url.txt
hex_line = binascii.hexlify(line.encode()).decode()
url_file_handle.write(hex_line + '\n')
else:
# 查找行中的 URL 地址并将匹配的行转换为十六进制后写入 url.txt
urls = url_pattern.findall(line)
if urls:
for url in urls:
hex_url = binascii.hexlify(url.encode()).decode()
url_file_handle.write(hex_url + '\n')
# 将其他行写入日志文件
log_file_handle.write(line)
log_file_handle.flush()
process.wait()
except subprocess.CalledProcessError as e:
# 捕获异常并记录到日志文件
logging.exception(f"程序发生异常: {e}")
except Exception as e:
# 捕获其他异常并记录到日志文件
logging.exception(f"程序发生异常: {e}")
finally:
# 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
logging.getLogger().removeHandler(console_handler)
console_handler.close() |