最新消息:

NSIS检测端口是否被占用

nsis 永恒心锁 3564浏览 0评论
!include "Ports.nsh"
XPStyle on
OutFile "Nsistest.exe"
Name "test"
Section "test"
${Unless} ${TCPPortOpen} 8000
 MessageBox MB_OK "httpd running"
${EndUnless}
${If} ${UDPPortOpen} 1337
 MessageBox MB_OK "leet port open :)"
${EndIf}
SectionEnd
# Usage:
# Push "Tcp" or "Udp"
# Push "port_number"
# Call IsPortOpen
# Pop $0 ; "open" or "closed" or anything else for error
#
# Or with the LogicLib
# ${If} ${TCPPortOpen} 80
# ${EndIf}
# ${If} ${UDPPortOpen} 137
# ${EndIf}
#
!include LogicLib.nsh
Function IsPortOpen
 Exch $R0 # port to check
 Exch
 Exch $R1
 Push $0
 Push $1
 Push $2
 System::Call 'iphlpapi::Get$R1Table(*i.r0, *i .r1, i 1) i .r2'
 ${If} $2 != 122 # ERROR_INSUFFICIENT_BUFFER
 StrCpy $R0 ""
 Pop $2
 Pop $1
 Pop $0
 Exch $R1
 Exch
 Exch $R0
 Return
 ${EndIf}
 System::Alloc $1
 Pop $0
 System::Call 'iphlpapi::Get$R1Table(ir0, *i r1, i 1) i .r2'
 ${If} $2 != 0 # NO_ERROR
 System::Free $0
 StrCpy $R0 ""
 Pop $2
 Pop $1
 Pop $0
 Exch $R1
 Exch
 Exch $R0
 Return
 ${EndIf}
 Push $3
 Push $4
 Push $5
 System::Call *$0(i.r2)
 IntOp $2 $2 - 1
 ${For} $3 0 $2
 IntOp $4 $0 + 4 # skip dwNumEntries
 ${If} $R1 == "Tcp"
 IntOp $5 $3 * 20 # sizeof(MIB_TCPROW)
 IntOp $4 $4 + $5 # skip to entry
 System::Call *$4(i.r1,i,i.r4,i,i)
 ${If} $1 <> 2 # $1 = dwState, 2 = MIB_TCP_STATE_LISTEN
 ${Continue}
 ${EndIf}
 ${Else}
 IntOp $5 $3 * 8 # sizeof(MIB_UDPROW)
 IntOp $4 $4 + $5 # skip to entry
 System::Call *$4(i,i.r4)
 ${EndIf}
 System::Call ws2_32::ntohs(ir4)i.r4
 ${If} $4 = $R0
 StrCpy $R0 "open"
 ${Break}
 ${EndIf}
 ${Next}
 ${If} $R0 != "open"
 StrCpy $R0 "closed"
 ${EndIf}
 System::Free $0
 Pop $5
 Pop $4
 Pop $3
 Pop $2
 Pop $1
 Pop $0
 Exch $R1
 Exch
 Exch $R0
FunctionEnd
# LogicLib macros for IsPortOpen
!macro _PortOpen _a _b _t _f
 !insertmacro _LOGICLIB_TEMP
 Push `${_a}`
 Push `${_b}`
 Call IsPortOpen
 Pop $_LOGICLIB_TEMP
 !insertmacro _== $_LOGICLIB_TEMP "open" `${_t}` `${_f}`
!macroend
!define PortOpen `PortOpen`
!macro _TCPPortOpen _a _b _t _f
 !insertmacro _PortOpen Tcp `${_b}` `${_t}` `${_f}`
!macroend
!define TCPPortOpen `"" TCPPortOpen`
!macro _UDPPortOpen _a _b _t _f
 !insertmacro _PortOpen Udp `${_b}` `${_t}` `${_f}`
!macroend
!define UDPPortOpen `"" UDPPortOpen`
免责声明:根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。丨本站为个人博客非盈利性站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途。丨本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权BY-NC-SA协议进行授权

转载请注明:永恒心锁-分享互联网 » NSIS检测端口是否被占用

您必须 登录 才能发表评论!