如何监控一个端口的状态
比如监控 2948 端口的状态是否为ESTABLISHED _WinAPI_GetTCPTable 可不可以给个示例啊... 汉化的udf里有例子 是哪个版本的,我的3.3.6.1里没有这个函数.. 说了,汉化的udf里 下载了那个汉化的UDF,依然没找到,郁闷... 帮帮忙哦... http://www.autoitx.com/thread-7404-1-1.html 以前在论坛收集的,你看看,希望对你有帮助#include <GUIConstants.au3>#include <GuiListView.au3>
#include <file.au3>
Local $DataArray,$Data
$AForm1 = GUICreate("本机开放端口查看", 730, 400, -1, -1)
$Listview1 = GUICtrlCreateListView("协议| 本地主机 |本地端口| 远程主机 | 远程端口 | 连接状态 |进程", 0, 0, 730, 400)
GUICtrlSendMsg($Listview1, 0x101E, 6, 259)
GUICtrlSendMsg(-1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
$Menu = GUICtrlCreateContextMenu($Listview1)
$Menu1 = GUICtrlCreateMenuItem("刷新", $Menu)
GUISetState(@SW_SHOW)
_ReNetstat()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Menu1
_ReNetstat()
EndSwitch
WEnd
Func _ReNetstat($LoReIP = True, $LoReIP1 = True)
GUICtrlSendMsg($Listview1, $LVM_DeleteALLITEMS, 0, 0)
RunWait(@ComSpec & ' /c netstat -ano > ' & @TempDir & '\NetSOT.tmp', "", @SW_HIDE)
If _FileReadToArray(@TempDir & '\NetSOT.tmp', $DataArray) <> 0 Then
For $I = 5 To $DataArray - 1
$StatColor = 1
$Data = StringReplace(StringMid($DataArray[$I], 1, 9), " ", "")
$Data = StringReplace(StringMid($DataArray[$I], 10, 23), " ", "")
$Prot = StringSplit($Data, ":")
If $Prot = 1 Then ExitLoop
$Data = $Prot;
If $LoReIP = False Then
If $Data = "127.0.0.1" Then ContinueLoop
EndIf
If $LoReIP1 = False Then
If $Data = "0.0.0.0" Then ContinueLoop
EndIf
$Data = $Prot ;
$Data = StringReplace(StringReplace(StringMid($DataArray[$I], 33, 23), " ", ""), "*:*", "0.0.0.0:0");data2
$Prot = StringSplit($Data, ":")
If $Prot = 1 Then
$Data = "";
$Data = "" ;
Else
$Data = $Prot;
$Data = $Prot ;
EndIf
$Data = StringReplace(StringMid($DataArray[$I], 56, 16), " ", "")
If $Data = "" Then
$Data = "侦听"
$StatColor = 0
Else
$Data = StringReplace(StringReplace(StringReplace(StringReplace($Data, "TIME_WAIT", "连接结束"), "LISTENING", "侦听"), "ESTABLISHED", "已连接"), "CLOSE_WAIT", "正在关闭");data3
EndIf
$Data = StringReplace(StringMid($DataArray[$I], 72, 10), " ", "");data4
$list = ProcessList()
For $J = 1 To $list
If $list[$J] = $Data Then
$Path = "System"
If $Data = 0 Then
$Path = "System"
Else
If $list[$J] <> "System" Then
$Path = _Processpath($Data)
If $Path = "" Then $Path = $list[$J]
EndIf
EndIf
$DisplayT = GUICtrlCreateListViewItem($Data & "|" & $Data & "|" & $Data & "|" & $Data & "|" & $Data & "|" & $Data & "|" & $Path, $Listview1)
If $StatColor = 1 Then GUICtrlSetBkColor(-1, 0xFFFED8)
EndIf
Next
Next
EndIf
FileDelete(@TempDir & "\NetSOT.tmp")
EndFunc ;==>_ReNetstat
Func _Processpath($iPID)
Local $aProc = DllCall('kernel32.dll', 'hwnd', 'OpenProcess', 'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $iPID)
If $aProc = 0 Then Return SetError(1, 0, '')
Local $vStruct = DllStructCreate('int')
DllCall('psapi.dll', 'int', 'EnumProcessModules', 'hwnd', $aProc, 'ptr', DllStructGetPtr($vStruct), 'int', DllStructGetSize($vStruct), 'int_ptr', 0)
Local $aReturn = DllCall('psapi.dll', 'int', 'GetModuleFileNameEx', 'hwnd', $aProc, 'int', DllStructGetData($vStruct, 1), 'str', '', 'int', 2048)
If StringLen($aReturn) = 0 Then Return SetError(2, 0, '')
Return $aReturn
EndFunc ;==>_Processpath 本帖最后由 lpxx 于 2010-11-14 13:15 编辑
你也可以参考蛋总汉化的UDF,
关于_WinAPI_GetTCPTable用法,你可以参考以下,现在的人真懒。
_WinAPI_GetTCPTable
获取IPv4 TCP端口的连接状态
#Include <WinAPIEx.au3>
_WinAPI_GetTCPTable()
参数
无.
返回值
成功: 包含下列元素的二维数组:
- 元素数量
- 连接状态:
1 - 无TCP连接
2 - TCP连接处于监听等待状态
3 - 已发送一个SYN包
4 - 已接收一个SYN包
5 - TCP连接已建立
6 - TCP连接正在等待一个FIN包
7 - TCP连接正在等待一个FIN包
8 - TCP连接处于结束等待状态
9 - TCP连接正在关闭
10 - TCP连接处于上次应答状态
11 - TCP连接处于等待状态
12 - TCP连接处于将被删除状态
- 本地IP
- 本地端口
- 远程IP
- 远程端口
失败: = -1
示例:#Include <Array.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global $TCPtable = _WinApi_GetTCPtable()
_ArrayDisplay($TCPtable)
页:
[1]