用AU3如何检测指定的端口是否打开或关闭
用AU3如何检测指定的端口是否打开或关闭 cmd:本机的监听信息:
netstat -na|findstr "LISTENING"
本机的连接信息:
netstat -na|findstr "ESTABLISHED" 官网上好像有这个函数,去找找看 给你一个检测的,懒得多找了
;~_GetTCPtable( ] )
;~
;~Return Value
;~Success: TCPtable[][] = 2-D array
;~ = number of connections
;~ for connection n:
;~ = connection state (integer)
;~ = local IP
;~ = local port
;~ = remote IP
;~ = remote port
;~ = connection state (informative text)
;~Failure: TCPtable = -1
Func _GetTCPtable($WSdll = "ws2_32.dll", $IHdll = "iphlpapi.dll")
Local Const $connState = ["CLOSED", "LISTENING", "SYN_SENT", "SYN_RCVD", "ESTABLISHED", "FIN_WAIT1", _
"FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT", "DELETE_TCB"]
Local $TCPtable = [[ -1]] ; preset to "failed"
$dwSize = DllStructCreate("dword") ; for MIB_TCPTABLE buffer size
$MIB_TCPTABLE = DllStructCreate("dword") ; nominal struct initially
DllStructSetData($dwSize, 1, 0) ; force zero size
$ret = DllCall($IHdll, "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get size
If @error Or $ret <> 122 Then Return $TCPtable ; dllCall error or RC is *not* ERROR_INSUFFICIENT_BUFFER = 122
$MIB_TCPTABLE = ""
For $i = 1 To DllStructGetData($dwSize, 1) / 4 ; make to requested size of buffer
$MIB_TCPTABLE &= "dword;"
Next
$MIB_TCPTABLE = DllStructCreate(StringTrimRight($MIB_TCPTABLE, 1)) ; requested struct
DllStructSetData($dwSize, 1, DllStructGetSize($MIB_TCPTABLE)) ; recheck its size
$ret = DllCall($IHdll, "int", "GetTcpTable", "ptr", DllStructGetPtr($MIB_TCPTABLE), "ptr", DllStructGetPtr($dwSize), "int", 1) ; get data
If @error Or $ret Then Return $TCPtable ; dllCall error or RC is Error
$numTCPentries = DllStructGetData($MIB_TCPTABLE, 1) ; number of entries
ReDim $TCPtable[$numTCPentries + 1]
For $i = 1 To $numTCPentries
$offset = ($i - 1) * 5 + 1 ; dword offset into struct
$TCPtable[$i] = DllStructGetData($MIB_TCPTABLE, $offset + 1) ; integer connection state
$TCPtable[$i] = $connState[$TCPtable[$i] - 1] ; connection state text
$ret = DllCall($WSdll, "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 2)) ; local IP / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i] = $ret
$ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 3)) ; local port / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i] = $ret
If $TCPtable[$i] <= 2 Then ; CLOSED or LISTENING state
$TCPtable[$i] = "0.0.0.0"
$TCPtable[$i] = 0
Else
$ret = DllCall($WSdll, "str", "inet_ntoa", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 4)) ; remote IP / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i] = $ret
$ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 5)) ; remote port / translate
If @error Then Return $TCPtable ; dllCall error
$TCPtable[$i] = $ret
EndIf
Next
$dwSize = 0
$MIB_TCPTABLE = 0
$TCPtable = $numTCPentries ; success
Return $TCPtable
EndFunc ;==>_GetTCPtable
;~_CloseTCPconnection( LocalIP, LocalPort, RemoteIP, RemotePort, ] )
;~
;~Return Value
;~Success: 1
;~Failure: 0
Func _CloseTCPconnection($localIP, $localPort, $remoteIP, $remotePort, $WSdll = "ws2_32.dll", $IHdll = "iphlpapi.dll")
$MIB_TCPROW = DllStructCreate("dword;dword;dword;dword;dword") ; connection struct
DllStructSetData($MIB_TCPROW, 1, 12) ; set to DELETE_TCB state = 12
$ret = DllCall($WSdll, "uint", "inet_addr", "str", $localIP) ; local IP / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 2, $ret)
$ret = DllCall($WSdll, "uint", "htons", "ushort", $localPort) ; local port / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 3, $ret)
$ret = DllCall($WSdll, "uint", "inet_addr", "str", $remoteIP) ; remote IP / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 4, $ret)
$ret = DllCall($WSdll, "uint", "htons", "ushort", $remotePort) ; remote port / translate
If Not @error Then DllStructSetData($MIB_TCPROW, 5, $ret)
$ret = DllCall($IHdll, "int", "SetTcpEntry", "ptr", DllStructGetPtr($MIB_TCPROW)) ; close connection
If @error Or $ret Then Return 0 ; dllCall error or RC is Error
$MIB_TCPROW = 0
Return 1 ; success
EndFunc ;==>_CloseTCPconnection 关注,楼上的给我具体示例吧,返回值说明下! 4楼的朋友可以直接给个例子出来吗? TCPStartUp()
$Socket = TCPConnect("220.181.28.52", 80)
If $socket <> 1 and $socket <>-1 and $socket <>2 Then
msgbox (32,"OK","端口是开放的!^_^")
Else
msgbox (16,"ERROR","发现错误了!^_^")
endif
TCPCloseSocket($Socket)
来源:
地址:http://l4ever.cn/archives/1087 我对这方面了解得不多,你看看这里吧
http://www.autoitscript.com/forum/index.php?showtopic=37304&hl=_GetTCPtable 7楼的朋友你试试FTP默认端口21和远程桌面端口3389 就知道,这段代码好象不行哦。 $objFirewall = objCreate("HNetCfg.FwMgr")
$objPolicy = $objFirewall.LocalPolicy.CurrentProfile
$objPolicy.FirewallEnabled = FALSE
; Enabling Remote Admin Port TCP 135
$objAdminSettings = $objPolicy.RemoteAdminSettings
$objAdminSettings.Enabled = TRUE
;Enable Port
Dim $objPort
$objPort = ObjCreate("HNetCfg.FwOpenPort")
$objPort.Name = "DCOM"
$objPort.Port = 888
$objPort.Scope = "NET_FW_SCOPE_LOCAL_SUBNET"
$objPort.Protocol = "NET_FW_IP_PROTOCOL_TCP"
$objPort.Enabled = True
$objProfile.GloballyOpenPorts.Add ($objPort)
再看看这段,不过有个缺陷,只检查已存在的指定端口
页:
[1]