Jump to content

Detecting what port a program is using.


Recommended Posts

Hello,

Regarding this old request here -> http://www.autoitscript.com/forum/index.ph...0351&hl=TCP

I'm searching for the same thing and since the code was outdated,I tried to update it and since I,myself am using XP sp3 I removed the check for Service pack,but still its not working!

Please help :)

#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <Constants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>

GUICreate("Open Port Information", 625, 342, -1, -1, BitOr($WS_SIZEBOX, $WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU))
$Open_Ports = GUICtrlCreateListView("Proto|Local Port|Remote Address:Port|State|PID|Process Name", 0, 23, 625, 280)
GUICtrlSetResizing(-1, $GUI_DOCKTOP + $GUI_DOCKBOTTOM)
GUICtrlSendMsg(-1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
_GUICtrlListView_SetColumnWidth ($Open_Ports, 0, 50)
_GUICtrlListView_SetColumnWidth ($Open_Ports, 1, 150)
_GUICtrlListView_SetColumnWidth ($Open_Ports, 2, 150)
_GUICtrlListView_SetColumnWidth ($Open_Ports, 3, 100)
_GUICtrlListView_SetColumnWidth ($Open_Ports, 4, 50)
_GUICtrlListView_SetColumnWidth ($Open_Ports, 5, 100)
$Btn_GetPorts = GUICtrlCreateButton("Get",(625/2) - 45,310,90,25)

GUISetState()

While 1
    $nMsg = GUIGetMsg ()
     
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $nMsg = $Btn_GetPorts
            _GetOpenPorts($Open_Ports)
    EndSelect
WEnd


Exit

Func _GetOpenPorts(ByRef $listview)
        $foo = Run(@ComSpec & " /c netstat -b -n", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;      $foo = Run(@ComSpec & " /c netstat -n -o", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    $OpenPorts = ""
    While 1
        $line = StdoutRead($foo)
        If @error = -1 Then ExitLoop
            Msgbox(0,"","DAMN ERROR :(  " & @error)
            $OpenPorts = $OpenPorts & $line
        Wend
        Msgbox(0,"","laaaaa" & @error & "damn!")
    $OpenPorts = StringSplit($OpenPorts,@CRLF, 1)
    For $x = $OpenPorts[0] To 1 Step - 1
        $OpenPorts[$x] = StringReplace($OpenPorts[$x],"127.0.0.1:","")
        If StringLen(String($OpenPorts[$x])) == 0 Or $OpenPorts[$x] == "Active Connections" Or StringInStr($OpenPorts[$x],"Address") Then
            _ArrayDelete($OpenPorts, $x)
            $OpenPorts[0] = UBound($OpenPorts) - 1
        Else
            $OpenPorts[$x] = StringTrimLeft($OpenPorts[$x], 2)
            If StringInStr($OpenPorts[$x],"]") Then
                $OpenPorts[$x]= StringReplace($OpenPorts[$x],"[","")
                $OpenPorts[$x] = StringReplace($OpenPorts[$x],"]","")
                $OpenPorts[$x - 1] = $OpenPorts[$x - 1] & " " & $OpenPorts[$x]
                _ArrayDelete($OpenPorts, $x)
                $OpenPorts[0] = UBound($OpenPorts) - 1
            EndIf
        EndIf
    Next
    Dim $Port[1]
   
    For $x = 1 To $OpenPorts[0]
        $Port = StringSplit($OpenPorts[$x]," ")
        $tmpStr = ""
        For $y = 1 To $Port[0]
            If StringIsASCII($Port[$y])Then
                $tmpStr = $tmpStr & $Port[$y] & "|"
            EndIf
        Next
        $OpenPorts[$x] = StringTrimRight($tmpStr, 1)
        $tmpList = StringSplit($OpenPorts[$x], "|")
        $tmpList[2] = StringMid($tmpList[2],StringInStr($tmpList[2],":") + 1,(StringLen($tmpList[2]) - (StringInStr($tmpList[2],":"))))
        $OpenPorts[$x] = ""
        For $y = 1 To $tmpList[0]
            $OpenPorts[$x] = $OpenPorts[$x] & $tmpList[$y] & "|"
        Next
        $OpenPorts[$x] = StringTrimRight($OpenPorts[$x],1)
        If $tmpList[0] == 5 Then
            $PList = ProcessList()
            For $y = 1 To $PList[0][0]
                $PList[$y][0] = StringReplace($PList[$y][0],"[","")
                $PList[$y][0] = StringReplace($PList[$y][0],"]","")
                If $PList[$y][1] == $tmpList[5] Then
                    $OpenPorts[$x] = $OpenPorts[$x] & "|" & $PList[$y][0]
                    ExitLoop
                EndIf
            Next
        EndIf
    Next
    $Port = 0
    _GUICtrlListView_DeleteAllItems($listview)
    For $x = 1 To $OpenPorts[0]
        GUICtrlCreateListViewItem($OpenPorts[$x],$listview)
    Next
EndFunc
Link to comment
Share on other sites

@H5O2OH

If @error = -1 Then ExitLoop

should be

If @error Then ExitLoop

also you can remove the two MsgBoxes.

the error returned by StdoutRead() is used to indicate end of file (EOF)

and exit the loop when no more data is returned from the console.

see the remarks for StdoutRead() in helpfile

I see fascists...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...