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
相关产品推荐
相关产品推荐

