You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyWin32实现的Windows端口反向转发服务在Local System账户下功能失效的问题咨询

PyWin32实现的Windows端口反向转发服务在Local System账户下功能失效的问题咨询

我用Python写了一个作为Windows服务运行的端口反向转发脚本,目的是把本地3389端口反向转发到远程服务器的55009端口。当用管理员账户登录运行这个服务时,一切都正常工作,但切换到Local System账户运行时,服务明明显示处于运行状态,可实际的端口转发功能完全没生效,有没有大佬能帮我分析下问题出在哪?

我已经把Python的安装路径/python和/python/Scripts都添加到系统环境变量里了,以下是我的代码:

import time
import sys
import subprocess as sp

import win32serviceutil  # ServiceFramework and commandline helper
import win32service  # Events
import servicemanager  # Simple setup and logging

class MyService:
    def stop(self):
        self.running = False

    def run(self):
        rport=55009
        # 反向转发端口的ssh命令
        cmd=f'ssh  -fNR {rport}:127.0.0.1:3389 username@remotehost -p 22'
        # 检查远程端口是否监听的命令
        cmd1='ssh -tt  username@remotehost -p 22  "netstat -tpa | grep \'**:{} **\'"'.format(rport)
        while True:
            # 执行检查命令并获取输出
            check_result=sp.run(cmd1,capture_output=True,shell=True).stdout.decode("utf-8") 
            # print('one turn')
            # 如果端口已在监听,休眠30秒再检查
            if f":{rport}" in check_result:
                time.sleep(30)
            # 如果端口没监听,重新启动转发命令
            else:
                sp.run(cmd,capture_output=True,shell=True)
                time.sleep(5)

备注:内容来源于stack exchange,提问作者wangwei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 08:28:13