76 lines
3.4 KiB
Python
76 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Windows 截图提供者:交互计划任务(LogonType Interactive)。
|
|
OpenSSH 服务会话(session 0)没有交互桌面,截图/前端输入必须在交互会话执行。
|
|
"""
|
|
import os
|
|
import subprocess
|
|
import time
|
|
|
|
USER = os.environ.get('USERNAME', 'yws-hhh')
|
|
BASE = os.path.join(os.path.expanduser('~'), 'lc_mcp')
|
|
os.makedirs(BASE, exist_ok=True)
|
|
OUT_DIR = r'C:\ProgramData\hduty'
|
|
os.makedirs(OUT_DIR, exist_ok=True)
|
|
JOB_PS1 = os.path.join(BASE, 'job.ps1')
|
|
RUN_VBS = os.path.join(BASE, 'run.vbs')
|
|
TASK = 'LcMCP'
|
|
|
|
if not os.path.exists(RUN_VBS):
|
|
with open(RUN_VBS, 'w', encoding='utf-8') as f:
|
|
f.write('CreateObject("WScript.Shell").Run "powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%s"", 0, False\n' % JOB_PS1)
|
|
|
|
|
|
def _pshell(code):
|
|
r = subprocess.run(['powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', code],
|
|
capture_output=True, text=True, encoding='utf-8', errors='replace', timeout=60)
|
|
return r.returncode, (r.stdout or ''), (r.stderr or '')
|
|
|
|
|
|
def _run_interactive(ps1_code, expect_file, timeout=15):
|
|
with open(JOB_PS1, 'w', encoding='utf-8') as f:
|
|
f.write('$ErrorActionPreference="Stop"\n' + ps1_code)
|
|
try:
|
|
if os.path.exists(expect_file):
|
|
os.remove(expect_file)
|
|
except Exception:
|
|
pass
|
|
reg = ("Unregister-ScheduledTask -TaskName %s -Confirm:$false -ErrorAction SilentlyContinue; "
|
|
"$A=New-ScheduledTaskAction -Execute 'wscript.exe' -Argument '%s'; "
|
|
"$P=New-ScheduledTaskPrincipal -UserId '%s' -LogonType Interactive -RunLevel Highest; "
|
|
"Register-ScheduledTask -TaskName %s -Action $A -Principal $P -Force | Out-Null; "
|
|
"Start-ScheduledTask -TaskName %s; 'OK'"
|
|
% (TASK, RUN_VBS.replace("'", "''"), USER, TASK, TASK))
|
|
rc, out, err = _pshell(reg)
|
|
if 'OK' not in out:
|
|
return {"ok": False, "error": "task start fail: %s %s" % (out[-150:], err[-150:])}
|
|
deadline = time.time() + timeout
|
|
while time.time() < deadline:
|
|
if os.path.exists(expect_file):
|
|
try:
|
|
with open(expect_file, 'r', encoding='utf-8') as f:
|
|
return {"ok": True, "out": f.read().strip()}
|
|
except Exception:
|
|
return {"ok": True, "out": ''}
|
|
time.sleep(0.4)
|
|
return {"ok": False, "error": "timeout waiting %s" % expect_file}
|
|
|
|
|
|
class WinScreenshotProvider:
|
|
|
|
def grab(self):
|
|
name = 'shot_%s.png' % time.strftime('%Y%m%d_%H%M%S')
|
|
img_path = os.path.join(OUT_DIR, name)
|
|
ps = ("Add-Type -AssemblyName System.Windows.Forms,System.Drawing; "
|
|
"$b=[System.Windows.Forms.Screen]::PrimaryScreen.Bounds; "
|
|
"$bmp=New-Object System.Drawing.Bitmap $b.Width,$b.Height; "
|
|
"$g=[System.Drawing.Graphics]::FromImage($bmp); "
|
|
"$g.CopyFromScreen($b.X,$b.Y,0,0,$b.Size); "
|
|
"$bmp.Save('%s'); $g.Dispose(); $bmp.Dispose(); "
|
|
"$f=Join-Path '%s' 'done.txt'; 'DONE' | Set-Content $f"
|
|
% (img_path, OUT_DIR))
|
|
r = _run_interactive(ps, os.path.join(OUT_DIR, 'done.txt'), timeout=20)
|
|
if not r.get('ok'):
|
|
return {'ok': False, 'error': r.get('error', 'screenshot fail')}
|
|
if not os.path.exists(img_path):
|
|
return {'ok': False, 'error': 'png not produced'}
|
|
return {'ok': True, 'path': img_path.replace('\\', '/')} |