【问题标题】:How to check the process running on specific port in NSIS如何检查 NSIS 中特定端口上运行的进程
【发布时间】:2012-05-06 22:58:02
【问题描述】:

我正在使用 NSIS 为我的应用程序开发安装程序。我需要对此安装程序做的是检查端口 80 是否可用,如果可用,请继续安装程序,如果它没有给出错误消息,其中包含在端口 80 上运行的进程名称。

我找到了一种方法来检查端口 80 是否可用。为此,我使用 Ports.nsh 作为插件。 http://nsis.sourceforge.net/Check_open_ports

${If} ${TCPPortOpen} 80
 MessageBox MB_OK|MB_ICONSTOP "PORT 80 is already using by another program..."
 Abort
${EndIf}

但是,我无法在该端口上找到正在运行的进程。我需要给出一条错误消息,例如

//Skype is running on port 80 and close Skype to continue with the installation. 

有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

    标签: installation nsis port80


    【解决方案1】:

    由于我在 nsis 中找不到合适的方法来执行此操作,因此我只使用 VBScript 来检查进程名称并通过 nsis 脚本调用它。以下是代码。

    //TestPort80.vbs

    Dim WshShell, oExec, key
    Set WshShell = CreateObject("WScript.Shell")
    Set oExec = WshShell.Exec("netstat -o -n -a")
    key = "0.0:80"
    
    Dim values
    values = checkPortStatus(oExec, key)
    portInUse = values(0)
    input = values(1)
    
    If portInUse Then
      x = InStrRev(input, " ")
    
      ProcessID = Mid(input, x+1)
    
      commandTxt = "tasklist /FI " & Chr(34) & "PID eq " & ProcessID & Chr(34)
    
    Dim oExec2, key2
    Set oExec2 = WshShell.Exec(commandTxt)
    key2 = ProcessID
    
    Dim values2
    values2 = checkPortStatus(oExec2, key2)
    Found = values2(0)
    input2 = values2(1)
    
    If Found Then
        y = InStr(input2, " ")
        ExeName = Left(input2, y-1)
            WScript.StdOut.WriteLine "Port 80 is using by " & ExeName
    End If
    End If
    '## If we explicitly set a Success code then we can avoid this.
    WScript.Quit 512
    
    Function checkPortStatus(oExec, key)
    portInUse = false
    input = ""
    Do While True
    
         If Not oExec.StdOut.AtEndOfStream Then
              input = oExec.StdOut.ReadLine()
              If InStr(input, key) <> 0 Then 
            ' Found Port 80
                    portInUse = true
                    Exit DO
              End If
         Else
            Exit DO
         End If
         WScript.Sleep 100
    Loop
    
    Do While oExec.Status <> 1
     WScript.Sleep 100
    Loop
    Dim values(1)
    values(0) = portInUse
    values(1) = input
    
    checkPortStatus = values
    
    End Function
    

    //Installer.nsi

    ${If} ${TCPPortOpen} 80
     GetTempFileName $0
         File /oname=$0 `TestPort80.vbs` 
         nsExec::ExecToStack `"$SYSDIR\CScript.exe" $0 //e:vbscript //B //NOLOGO`
     Pop $0
     Pop $1
     MessageBox MB_OK|MB_ICONSTOP '$1'
     Abort
    ${EndIf}
    

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多