Jump to content

Port Check function


kko
 Share

Recommended Posts

At some point I needed to add a port checker to an Administrator's aid I have been using for some time to check for specific open / forgotten ports.

Just thought I would share it with you. (I am pretty sure there are better scripts available somewhere in this forum)

;demo result
msgbox(0,"",PortCheck("172.21.25.1"))


;the actual function
Func PortCheck($py = "127.0.0.1",$px="all")
    Local $maxport = 14 ;set maximum of array / change this to fit the array if u r going to add more ports
    Dim $PortList[$maxport][2]

$PortList[0][0] = 80 ;HTTP
$PortList[0][1] = "HTTP"
$PortList[1][0] = 81 ;HTTP alt
$PortList[1][1] = "HTTP alt"
$PortList[2][0] = 8080 ;HTTP alt
$PortList[2][1] = "HTTP alt"
$PortList[3][0] = 3389 ;RDP
$PortList[3][1] = "Terminal Services - RDP"
$PortList[4][0] = 5900 ;VNC
$PortList[4][1] = "VNC Server Port"
$PortList[5][0] = 1433 ;MS SQL Listener
$PortList[5][1] = "MSSQL Server Database Engine"
$PortList[6][0] = 20 ; FTP data
$PortList[6][1] = "FTP Data"
$PortList[7][0] = 21 ; FTP Control
$PortList[7][1] = "FTP Control/Data"
$PortList[8][0] = 22 ; SSH
$PortList[8][1] = "SSH"
$PortList[9][0] = 2483 ;Oracle Listener Server
$PortList[9][1] = "Oracle Listener Server"
$PortList[10][0] = 3306 ; Mysql Database System
$PortList[10][1] = "Mysql Database System"
$PortList[11][0] = 5500 ;VNC Incoming Listener
$PortList[11][1] = "VNC Incoming Listener"
$PortList[12][0] = 3872 ;Oracle management tool
$PortList[12][1] = "Oracle management tool"
$PortList[13][0] = 23 ;Telnet
$PortList[13][1] = "Telnet"

    Opt('TCPTimeout', 100)
    TCPStartup() ;Start TCP services
        Local $testport, $pi =0,$port_result = "Ports"

    If $px = "all" Then

        while $pi<$maxport
        $testport = TCPConnect($py,$PortList[$pi][0])
        TCPCloseSocket($PortList[$pi][0])
        If $testport>0 Then $port_result = $port_result & ", " & $PortList[$pi][0] & " (" & $PortList[$pi][1] & ")"
        $pi = $pi + 1 ;go to next port
        WEnd
    Else ;if specific port
        $testport = TCPConnect($py,$px)
        TCPCloseSocket($px)
        If $testport>0 Then $port_result = $port_result & ", " & $px
    EndIf



    $PortList = 0 ;erase list
    $port_result = StringReplace($port_result,"Ports,", "Ports:") ;make result string look nice
    TCPShutdown ( ) ; Close TCP Services
    Return $port_result
EndFunc

That's it. Sincerely hope somebody finds it useful. Cheers.

check_4_open_port.au3

Link to comment
Share on other sites

When i check VNCViewer listner, it show a "VNCViewer message box".

Do it is possible to aoid?

hmm yes never thought abt that... it probably shows a bad connection or connection interrupted since VNC Viewers on listener mode don't have any negotiation protocol (connection goes directly for authentication)... Which VNC programme do you use?

Possible solution I am thinking ...is about sending a closed gracefully or wrong username/password if connection successful so that you don't get the error message.

Link to comment
Share on other sites

  • 5 years later...

I realize that this topic is pretty old.

However, the presented script is so misleading that I feel compelled to comment:

The use of the TCPCloseSocket() function is utterly wrong! This function explicitly specifies that the input parameter should be a valid Socket Descriptor! In the script above, the function is fed with a port number. In the script above, such an error is negligible, but in real life it won't work and network resources would not be freed!!!

I also want to emphasize that using the TCPConnect(), to check for a free port, is utterly wasteful. In the least, I would invoke the "netstat" command line utility, capture its output and then analyze it, instead. Better still, one should employ the proper network API services to scan for free or occupied ports, just as "netstat" does.

I hope my comment may be still valuable to some and save toil from others.

Edited by Turing
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...