如何判读本地主机与远程主机的某个端口已经建立网络连接了?
本帖最后由 aux649350702 于 2010-12-23 09:06 编辑如何判读本地主机与远程主机的某个端口已经建立网络连接了,比如,本机主机ip:192.168.153.242,远程主机ip:192.168.151.15,远程主机提供了某个服务,使用6665端口,如果连接已经建立则通过netstat /an 就可以看到:
ProtoLocal Address Foreign Address State
TCP 192.168.153.174:1025 192.168.151.15:3260 ESTABLISHED
,给出服务器ip和端口作参数,怎么判断本地主机已经和服务器的那个端口建立了网络连接了? 是这个吗? 回复 2# netegg
能知道连接是哪个进程发出的吗? 这是我收集的,非原创,作者也不知道了。
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <file.au3>
Global $DataArray, $Data
$TCPFORM_1 = GUICreate("网络连接查看", 698, 377, -1, -1)
$TCPList_1 = GUICtrlCreateListView("", 0, 0, 698, 377)
_GUICtrlListView_SetExtendedListViewStyle(-1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSelect, $LVS_EX_SUBITEMIMAGES))
_GUICtrlListView_AddColumn(-1, "进程", 100)
_GUICtrlListView_AddColumn(-1, "Pid", 50)
_GUICtrlListView_AddColumn(-1, "协议", 50)
_GUICtrlListView_AddColumn(-1, "本地端口", 115)
_GUICtrlListView_AddColumn(-1, "远程端口", 100)
_GUICtrlListView_AddColumn(-1, "连接状态", 80)
_GUICtrlListView_AddColumn(-1, "程序路径", 130)
GUICtrlSetImage($TCPList_1, "shell32.dll", -3)
$TCPmenu1 = GUICtrlCreateContextMenu($TCPList_1)
$TCPmenuC1 = GUICtrlCreateMenuItem("刷新列表", $TCPmenu1)
$TCPmenuC2 = GUICtrlCreateMenuItem("结束进程", $TCPmenu1)
GUISetState(@SW_SHOW)
_read()
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($TCPFORM_1)
ExitLoop
Case $TCPmenuC1
_read()
Case $TCPmenuC2
$TCPdu = GUICtrlRead(GUICtrlRead($TCPList_1))
$TCPdu_1 = StringSplit($TCPdu, "|")
If GUICtrlRead($TCPList_1) <> 0 Then
ProcessClose($TCPdu_1)
_read()
EndIf
EndSwitch
WEnd
Func _read()
GUICtrlSendMsg($TCPList_1, $LVM_DeleteALLITEMS, 0, 0)
_ReNet()
EndFunc ;==>_read
Func _ReNet($LoReIP = True, $LoReIP1 = True)
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), " ", ""), "*:*", "*:*");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", "TIME_WAIT"), "LISTENING", "LISTENING"), "ESTABLISHED", "ESTABLISHED"), "CLOSE_WAIT", "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($list[$J] & "|" & $list[$J] & "|" & $Data & "|" & $Data & ":" & $Data & "|" & $Data & ":" & $Data & "|" & $Data & "|" & $Path, $TCPList_1)
GUICtrlSetImage($DisplayT, $Path, 0)
EndIf
Next
Next
EndIf
FileDelete(@TempDir & "\NetSOT.tmp")
EndFunc ;==>_ReNet
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 懒得给你一个个拆函数了,直接给你个老外的脚本吧 呵呵,这个问题看来,只能调用netstat /an 了。 上面代码应该满足你的需求吧
页:
[1]