vbs check port status on local machine

Dim isOccupied, port
isOccupied = 0
  
Do
    port = InputBox("输入要检查的端口")
    If IsEmpty(port) Then    'Cancel button is clicked
        WScript.Quit
    ElseIf Len(port) = 0 Then
        MsgBox "No input!", vbExclamation + vbSystemModal
    ElseIf Not IsNumeric(port) Then
        MsgBox "Must input a port number!", vbExclamation + vbSystemModal
    Else
        Exit Do
    End If
Loop
 
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("netstat -an")
Set oStdOut = oExec.StdOut
Do Until oStdOut.AtEndOfStream
    strLine = oStdOut.ReadLine
    WScript.echo strLine
    If InStr(strLine, ":" & port) > 0 And InStrRev(strLine, "LISTENING") > 0 Then
        isOccupied = 1
        Exit Do
    End If
Loop

If isOccupied Then
    MsgBox "端口 " & port & "已经被占用", vbExclamation + vbSystemModal, "Busy"
Else
    MsgBox "端口 " & port & " 未被占用", vbInformation + vbSystemModal, "Idle"
End If

 

 

还有一种 写法, 这种需要计算机上注册mswinsck.ocs 控件,大多数的计算机并没有这个控件,因此下面的这个方法不具有通用性

 

 

'' check the port status from specified server
'' server : host name or ip address
'' port : port number
Function getPortStatus(server,port)
    Set SockObject=CreateObject("MSWinsock.Winsock.1")
    SockObject.Protocol=0 ' TCP
    SockObject.Connect server,port
    Wscript.Sleep 50
    If(action = 6) then
        getPortStatus = "Invalid"
    ElseIf(SockObject.State = 7) then
        getPortStatus = "Open"
    ElseIf(SockObject.State = 9)then
        getPortStatus = "Error"
    ElseIf(SockObject.State = 0)then 'Closed
        getPortStatus = "Refused"
    Else
          getPortStatus = "Error"
    end If
    SockObject.Close
End Function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值