Jump to content

Testing connections to Remote Server TCP Ports


Go to solution Solved by sahsanu,

Recommended Posts

Hi all,

I've been away from AutoIT for a bit but have to update/improve a tool I've developed in the past.  One of the features I need to add is the ability to test connections to a remote server on specific TCP ports.  In manual testing we do this now by opening a command prompt and typing in "telnet 192.168.1.10:135" and seeing if it times out or connects, there is no data or prompt returned when a successful connection is established and only a bit of information returned in the command prompt if it doesn't, i.e. connect failed and being returned to a prompt.  We are only looking to see if a connection is successful or fails, no data (username, password, commands, etc.) have to be passed back and forth. 

I have a bit of working code, but it is clunky (relying on sleeps) and takes longer to complete than it does just to open cmd type in telnet and do this manually.  I'm sure there has to be a better way to do this, but my searches haven't lead me to any good solutions.  I've looked at using Putty, Tera Term and even Console Telnet that PincoPanco suggested in this thread '?do=embed' frameborder='0' data-embedContent>> but haven't had any success.  Does anyone have any pointers on an easy way to do this?

Thanks for any help.

Dave

#include <WinAPIFiles.au3>
Opt("WinDetectHiddenText", 1)
$ODBC = "LocalHost"
$ODBC_IP = "10.10.1.10"
$RPC_Port = "135"
$SQL_Port = "1825"
$LS_Port = "1234"



_WinAPI_Wow64EnableWow64FsRedirection(False)
    $PID1 = Run("Telnet " & $ODBC_IP & " " & $RPC_Port)
        Sleep(30000)
    If WinExists("Telnet " & $ODBC_IP) Then
        $Telnet_RPC = ("Connected")
        WinClose("Telnet " & $ODBC_IP)
    Else
        $Telnet_RPC = ("Failed")
    EndIf

    ;Sleep (5000)

    $PID2 = Run("Telnet " & $ODBC_IP & " " & $SQL_Port)
        Sleep(30000)
    If WinExists("Telnet " & $ODBC_IP) Then
        $Telnet_SQL = ("Connected")
        WinClose("Telnet " & $ODBC_IP)
    Else
        $Telnet_SQL = ("Failed")
    EndIf

    ;Sleep (5000)

    $PID3 = Run("Telnet " & $ODBC_IP & " " & $LS_Port)
        Sleep(30000)
    If WinExists("Telnet " & $ODBC_IP) Then
        $Telnet_License_Server = ("Connected")
        WinClose("Telnet " & $ODBC_IP)
    Else
        $Telnet_License_Server = ("Failed")
    EndIf
_WinAPI_Wow64EnableWow64FsRedirection(True)
    Msgbox (1, "RPC Status", $Telnet_RPC,0)
    Msgbox (1, "SQL Status", $Telnet_SQL,0)
    Msgbox (1, "LS Status", $Telnet_License_Server,0)
Link to comment
Share on other sites

Take a look to TCPConnect, I think the example script has all the info you need to accomplish the task.

 

Sahsanu,

Thank you so much!  You made this very, very simple for me.  I knew there had to be a better way, but never even saw TCPConnect in the help. 

Thanks again,

Dave

Link to comment
Share on other sites

  • Solution

Sahsanu,

Thank you so much!  You made this very, very simple for me.  I knew there had to be a better way, but never even saw TCPConnect in the help. 

Thanks again,

Dave

 

You are welcome ;)

Just an example using your first script:

Local $ODBC = "LocaHost"
Local $ODBC_IP = "10.10.1.10"
Local $RPC_Port = "135"
Local $SQL_Port = "1825"
Local $LS_Port = "1234"
Local $aPorts = [$RPC_Port, $SQL_Port, $LS_Port]

TCPStartup()
For $i = 0 To UBound($aPorts) - 1
    $iSocket = TCPConnect($ODBC_IP, $aPorts[$i])
    If @error Then
        ConsoleWrite("No luck connecting to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    Else
        ConsoleWrite("Yeah, I've connected to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    EndIf
    TCPCloseSocket($iSocket)
Next
TCPShutdown()
Link to comment
Share on other sites

 

You are welcome ;)

Just an example using your first script:

Local $ODBC = "LocaHost"
Local $ODBC_IP = "10.10.1.10"
Local $RPC_Port = "135"
Local $SQL_Port = "1825"
Local $LS_Port = "1234"
Local $aPorts = [$RPC_Port, $SQL_Port, $LS_Port]

TCPStartup()
For $i = 0 To UBound($aPorts) - 1
    $iSocket = TCPConnect($ODBC_IP, $aPorts[$i])
    If @error Then
        ConsoleWrite("No luck connecting to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    Else
        ConsoleWrite("Yeah, I've connected to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    EndIf
    TCPCloseSocket($iSocket)
Next
TCPShutdown()

Amazing!!!!!!  Thank you, it works quickly and exactly as it should.  I appreciate it and can use this example to finish up the tool I was working on. 

Thanks again, I really appreciate it.

Dave

Link to comment
Share on other sites

 

You are welcome ;)

Just an example using your first script:

Local $ODBC = "LocaHost"
Local $ODBC_IP = "10.10.1.10"
Local $RPC_Port = "135"
Local $SQL_Port = "1825"
Local $LS_Port = "1234"
Local $aPorts = [$RPC_Port, $SQL_Port, $LS_Port]

TCPStartup()
For $i = 0 To UBound($aPorts) - 1
    $iSocket = TCPConnect($ODBC_IP, $aPorts[$i])
    If @error Then
        ConsoleWrite("No luck connecting to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    Else
        ConsoleWrite("Yeah, I've connected to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    EndIf
    TCPCloseSocket($iSocket)
Next
TCPShutdown()

Sahsanu and all;

I found an issue that I think is related to TCP Connect.  If the port I am looking for is open and a connection can be established it works very nicely, but if a connection is not available then it takes some time (about 10 seconds per port) to time out.   I've tried the Opt("TCPTimeout", 100) option but that doesn't seem to make any difference.  I also found this '?do=embed' frameborder='0' data-embedContent>> and tried it but I can't seem to figure out how to tell if it succeeds or not. 

Any help would be much appreciated.

P.S.  I marked this unsolved as well, since I now have another small issue.

P.S.S. I found a sample that does work for me with a timeout. 

Thanks,

Dave

Edited by dbs179
Link to comment
Share on other sites

Copy the Function _TCPConnect from >JScript post which is this one:

; #FUNCTION# ====================================================================================================================
; Name...........: _TCPConnect
; Description ...: Triess to establishes a TCP-connection in a specified time limit
; Syntax.........: _TCPConnect($sIPAddr, $iPort, $iTimeOut = -1)
; Parameters ....: $sIpAddr - IP address to connect to (IPv4)
; $iPort - Port to use
; $iTimeOut - Timeout for connection in milliseconds (default: -1)
; |Values < 0: default timeout
; |Values 0, Keyword Default: use time from Opt("TCPTimeout")
; |Values > 0: timeout in milliseconds
; Return values .: Success - Socket to use with TCP-functions
; Failure - -1, sets @error
; |1 - $sIpAddr incorrect
; |2 - could not get port
; |3 - could not create socket
; |4 - could not connect
; |5 - could not get WSAError
; |and errors from WSAGetLastError
; Author ........: ProgAndy
; Modified.......: JScript
; Remarks .......:
; Related .......: TCPConnect, TCPCloseSocket, TCPSend, TCPRecv
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _TCPConnect($sIPAddr, $iPort, $iTimeOut = -1)
    Local $hWs2 = DllOpen("Ws2_32.dll")
    Local $iDllErr, $fError = False, $aRes
    Local $hSock = DllCall($hWs2, "uint", "socket", "int", 2, "int", 1, "int", 6)
    If @error Then
        $iDllErr = 3
    ElseIf $hSock[0] = 4294967295 Or $hSock[0] = -1 Then
        $fError = True
    Else
        $hSock = $hSock[0]
        $aRes = DllCall($hWs2, "ulong", "inet_addr", "str", $sIPAddr)
        If @error Or $aRes[0] = -1 Or $aRes[0] = 4294967295 Then
            $iDllErr = 1
        Else
            $iPort = DllCall($hWs2, "ushort", "htons", "ushort", $iPort)
            If @error Then
                $iDllErr = 2
            Else
                $iPort = $iPort[0]
            EndIf
        EndIf
        If 0 = $iDllErr Then
            Local $tSockAddr = DllStructCreate("short sin_family;ushort sin_port; ulong sin_addr;char sin_zero[8];")
            DllStructSetData($tSockAddr, 1, 2)
            DllStructSetData($tSockAddr, 2, $iPort)
            DllStructSetData($tSockAddr, 3, $aRes[0])

            If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")

            If $iTimeOut > -1 Then DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 1)
            $aRes = DllCall($hWs2, "int", "connect", "int", $hSock, "ptr", DllStructGetPtr($tSockAddr), "int", DllStructGetSize($tSockAddr))

            Select
                Case @error
                    $iDllErr = 4
                Case $aRes[0] <> 0
                    $aRes = DllCall($hWs2, "int", "WSAGetLastError")
                    If Not @error And $aRes[0] = 10035 Then ContinueCase
                    $fError = True
                Case $iTimeOut > -1
                    If IsKeyword($iTimeOut) Or $iTimeOut = 0 Then $iTimeOut = Opt("TCPTimeout")
                    Local $t = DllStructCreate("uint;int")
                    DllStructSetData($t, 1, 1)
                    DllStructSetData($t, 2, $hSock)
                    Local $to = DllStructCreate("long;long")
                    DllStructSetData($to, 1, Floor($iTimeOut / 1000))
                    DllStructSetData($to, 2, Mod($iTimeOut, 1000))
                    $aRes = DllCall($hWs2, "int", "select", "int", $hSock, "ptr", DllStructGetPtr($t), "ptr", DllStructGetPtr($t), "ptr", 0, "ptr", DllStructGetPtr($to))
                    If Not @error And $aRes[0] = 0 Then
                        $aRes = DllCall($hWs2, "int", "WSAGetLastError")
                        If Not @error And $aRes[0] = 0 Then
                            $iDllErr = 10060
                        Else
                            $fError = True
                        EndIf
                    Else
                        DllCall($hWs2, "int", "ioctlsocket", "int", $hSock, "long", 0x8004667e, "uint*", 0)
                    EndIf
            EndSelect
        EndIf
    EndIf
    If $iDllErr Then
        TCPCloseSocket($hSock)
        $hSock = -1
    ElseIf $fError Then
        $iDllErr = DllCall($hWs2, "int", "WSAGetLastError")
        If Not @error Then $iDllErr = $iDllErr[0]
        If $iDllErr = 0 Then $iDllErr = 5
        TCPCloseSocket($hSock)
        $hSock = -1
    EndIf
    DllClose($hWs2)
    Return SetError($iDllErr, 0, $hSock)
EndFunc   ;==>_TCPConnect

And save it to a file named _TCPConnect.au3

And use this sample script to test it:

#include "_TCPConnect.au3"

Local $ODBC = "LocaHost"
Local $ODBC_IP = "10.0.0.1"
Local $RPC_Port = "135"
Local $SQL_Port = "1825"
Local $LS_Port = "1234"
Local $aPorts = [$RPC_Port, $SQL_Port, $LS_Port]

TCPStartup()
For $i = 0 To UBound($aPorts) - 1
    $iSocket = _TCPConnect($ODBC_IP, $aPorts[$i],100)
    If @error Then
        ConsoleWrite("No luck connecting to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    Else
        ConsoleWrite("Yeah, I've connected to " & $ODBC & "/" & $ODBC_IP & " on port " & $aPorts[$i] & @CRLF)
    EndIf
Next
TCPShutdown()

Cheers,

sahsanu

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...