Sign in to follow this  
Followers 0
Morthawt

See if a port is open for business vs ping

3 posts in this topic

Is it possible to do a simple code to just see if an IP is accepting connections on a given port rather than pinging an IP to see if the computer its self is up? I want to make a program to check the status of a server while keeping the server secure from ICMP ping traffic. I need it to just test to see if a connection is possible and report back if it can connect or not. No fancy commands no receiving data over the connection or anything.. Just identify if the port is indeed open.

Is this possible?

Share this post


Link to post
Share on other sites



Can you try this?

;Coded by UEZ 2010
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0
#AutoIt3Wrapper_Run_After=del /f /q "Port_Scanner_Obfuscated.au3"
#AutoIt3Wrapper_Run_After=upx.exe --ultra-brute "%out%"
;~ #AutoIt3Wrapper_Run_After=upx.exe --best "%out%"

Local $version = "Port_Scanner v0.25 Beta Build 2010-08-14 by UEZ d-¿-b"
Local $ip, $start_port, $end_port, $socket, $code, $x

If $CmdLine[0] = 0 Then Display_Help()

If $CmdLine[0] > 0 Then
    If $CmdLine[1] = "-h" Or $CmdLine[1] = "/?" Or $CmdLine[1] = "-?" Or $CmdLine[0] = 1 Then Display_Help()

    If $CmdLine[0] > 1 And StringIsInt($CmdLine[2]) And $CmdLine[2] > -1 And $CmdLine[2] < 0x10000 Then
        $start_port = $CmdLine[2]
    Else
        Display_Help()
    EndIf

    If $CmdLine[0] > 2 Then
        If StringIsInt($CmdLine[3]) And $CmdLine[3] > $CmdLine[2] And $CmdLine[3] < 0x10000 Then
            $end_port = $CmdLine[3]
        Else
            Display_Help()
        EndIf
    EndIf
EndIf

If Not StringIsFloat($CmdLine[1]) Or Not StringIsAlpha($CmdLine[1]) Then
    If Ping($CmdLine[1]) Then
        $ip = $CmdLine[1]
    Else
        _Exit(1, @CRLF & 'Host "' & $CmdLine[1] & '" not reachable!' & @CRLF)
    EndIf
Else
    Display_Help()
EndIf

TCPStartUp()
If Not $end_port Then
    $socket = TCPConnect($ip, $start_port)
    If $socket = -1 Then
        ConsoleWrite(@CRLF & "Error connecting to port " & $start_port & ": " & $socket & @CRLF)
        _Exit(1)
    EndIf
    ConsoleWrite(@CRLF & "Socket identifier on TCP port " & $start_port & ": " & $socket & @CRLF & @CRLF)
    TCPCloseSocket($socket)
    _Exit()
Else
    ConsoleWrite(@CRLF)
    For $x = $start_port to $end_port
        $socket = TCPConnect($ip, $x)
        If $socket = -1 Then
            ConsoleWrite("Error connecting to port " & $x & ": " & $socket & @CRLF)
        Else
            ConsoleWrite("Socket identifier on TCP port " & $x & ": " & $socket & @CRLF)
            TCPCloseSocket($socket)
        EndIf
    Next
    ConsoleWrite(@CRLF)
EndIf
_Exit()

Func _Exit($code = 0, $msg = "")
    TCPShutdown()
    ConsoleWrite($msg & @CRLF)
    Exit($code)
EndFunc

Func Display_Help()
    ConsoleWrite(@CRLF & $version & @CRLF)
    ConsoleWrite(@CRLF & "Syntax: Port_Scanner.exe <ip> <start port> [end port]" & @CRLF & @CRLF)
    ConsoleWrite("Examples:" & @TAB &  "Port_Scanner.exe 127.0.0.1 80" & @CRLF & @TAB & @TAB & "Port_Scanner.exe 127.0.0.1 80 100" & @CRLF & @CRLF)
    ConsoleWrite("TCP Port range:" &  @TAB &  "0 - 65535" & @CRLF & @CRLF)
    Exit
EndFunc

Compile it and start it from CMD.

I had never the time to test it properly.

Thanks,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Well I was thinking more small like:

Opt('TCPTimeout', 500)
TCPStartup()
$grimdal = TCPConnect('127.0.0.1' , 16502 )
TCPCloseSocket($test)
$uslogin = TCPConnect('127.0.0.1' , 21002 )
TCPCloseSocket($uslogin)
MsgBox(0,0,$test)
MsgBox(0,0,$uslogin)

So far this is working but when the port on the server is not listening it takes ages before it times out.

Share this post


Link to post
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
Sign in to follow this  
Followers 0