找回密码
 加入
搜索
查看: 3528|回复: 9

用AU3如何检测指定的端口是否打开或关闭

[复制链接]
发表于 2008-10-27 10:07:23 | 显示全部楼层 |阅读模式
用AU3如何检测指定的端口是否打开或关闭
发表于 2008-10-27 10:26:41 | 显示全部楼层
cmd:

本机的监听信息:
netstat -na|findstr "LISTENING"

本机的连接信息:
netstat -na|findstr "ESTABLISHED"
发表于 2008-10-27 13:06:42 | 显示全部楼层
官网上好像有这个函数,去找找看
发表于 2008-10-27 13:15:04 | 显示全部楼层
给你一个检测的,懒得多找了
;~  _GetTCPtable( [optional handle to "ws2_32.dll" [, optional handle to "iphlpapi.dll" ] ] )
;~
;~  Return Value
;~  Success: TCPtable[][] = 2-D array
;~      [0][0] = number of connections
;~     for connection n:
;~      [n][0] = connection state (integer)
;~      [n][1] = local IP
;~      [n][2] = local port
;~      [n][3] = remote IP
;~      [n][4] = remote port
;~      [n][5] = connection state (informative text)
;~  Failure: TCPtable[0][0] = -1

Func _GetTCPtable($WSdll = "ws2_32.dll", $IHdll = "iphlpapi.dll")

    Local Const $connState[12] = ["CLOSED", "LISTENING", "SYN_SENT", "SYN_RCVD", "ESTABLISHED", "FIN_WAIT1", _
            "FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT", "DELETE_TCB"]
   
    Local $TCPtable[1][1] = [[ -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[0] <> 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[0] Then Return $TCPtable ; dllCall error or RC is Error

    $numTCPentries = DllStructGetData($MIB_TCPTABLE, 1) ; number of entries
    ReDim $TCPtable[$numTCPentries + 1][6]
   
    For $i = 1 To $numTCPentries
        $offset = ($i - 1) * 5 + 1 ; dword offset into struct
        $TCPtable[$i][0] = DllStructGetData($MIB_TCPTABLE, $offset + 1) ; integer connection state
        $TCPtable[$i][5] = $connState[$TCPtable[$i][0] - 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][1] = $ret[0]
        $ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 3)) ; local port / translate
        If @error Then Return $TCPtable ; dllCall error
        $TCPtable[$i][2] = $ret[0]
      
        If $TCPtable[$i][0] <= 2 Then ; CLOSED or LISTENING state
            $TCPtable[$i][3] = "0.0.0.0"
            $TCPtable[$i][4] = 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][3] = $ret[0]
            $ret = DllCall($WSdll, "ushort", "ntohs", "uint", DllStructGetData($MIB_TCPTABLE, $offset + 5)) ; remote port / translate
            If @error Then Return $TCPtable ; dllCall error
            $TCPtable[$i][4] = $ret[0]
        EndIf
    Next

    $dwSize = 0
    $MIB_TCPTABLE = 0
    $TCPtable[0][0] = $numTCPentries ; success
    Return $TCPtable
   
EndFunc   ;==>_GetTCPtable

;~  _CloseTCPconnection( LocalIP, LocalPort, RemoteIP, RemotePort, [optional handle to "ws2_32.dll" [, optional handle to "iphlpapi.dll"]] )
;~
;~  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[0])
    $ret = DllCall($WSdll, "uint", "htons", "ushort", $localPort) ; local port / translate
    If Not @error Then DllStructSetData($MIB_TCPROW, 3, $ret[0])

    $ret = DllCall($WSdll, "uint", "inet_addr", "str", $remoteIP) ; remote IP / translate
    If Not @error Then DllStructSetData($MIB_TCPROW, 4, $ret[0])
    $ret = DllCall($WSdll, "uint", "htons", "ushort", $remotePort) ; remote port / translate
    If Not @error Then DllStructSetData($MIB_TCPROW, 5, $ret[0])

    $ret = DllCall($IHdll, "int", "SetTcpEntry", "ptr", DllStructGetPtr($MIB_TCPROW)) ; close connection
    If @error Or $ret[0] Then Return 0 ; dllCall error or RC is Error
   
    $MIB_TCPROW = 0
    Return 1 ; success
   
EndFunc   ;==>_CloseTCPconnection
发表于 2008-10-27 13:48:11 | 显示全部楼层
关注,楼上的给我具体示例吧,返回值说明下!
 楼主| 发表于 2008-10-27 16:44:59 | 显示全部楼层
4楼的朋友可以直接给个例子出来吗?
发表于 2008-10-27 17:14:30 | 显示全部楼层
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
发表于 2008-10-28 00:00:14 | 显示全部楼层
我对这方面了解得不多,你看看这里吧
http://www.autoitscript.com/foru ... amp;hl=_GetTCPtable
 楼主| 发表于 2008-10-28 08:10:40 | 显示全部楼层
7楼的朋友你试试FTP默认端口21和远程桌面端口3389 就知道,这段代码好象不行哦。
发表于 2008-10-28 09:42:38 | 显示全部楼层
$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)
再看看这段,不过有个缺陷,只检查已存在的指定端口
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-16 04:13 , Processed in 0.075659 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表