Jump to content
Sign in to follow this  
Ticki84

Ping a specific port

Recommended Posts

Ticki84

Hello everyone,

I search how to Ping() an specific port.

I use it to get a server status.

This work:

Ping("servers.com", 5000)

But this doesn't work:

Ping("servers.com:12345", 5000)

If someone could help me, it's would be wonderful.

Share this post


Link to post
Share on other sites
mlukac89

U see in help file u cant make a port only adress or ip

Parameters

address/hostname Can be i.e. "www.autoitscript.com" or "87.106.244.38".

timeout [optional] Is the time to wait for an answer in milliseconds (default is 4000).

Try this

or use TCP or UDP

Edited by mlukac89

Share this post


Link to post
Share on other sites
Ticki84

It doesn't work, it always return -1 with default address (from the script) and with the custom adress (from servers).

There's the code if you want it:

#include <File.au3>
#include <Misc.au3>

Local $Server1 = _PingPort("servers.com", 25001)
Local $Server2 = Ping("servers.com", 14050)
Local $SServer1 = 0
Local $SServer2 = 0

Func _PingPort($ip, $port)
    ; Connect to server
    $nSock = TCPConnect($ip, $port)
    if ($nSock <= -1) Then return -1

    ; Fill up 32 bytes
    $sBytes = ""
    For $i = 1 to 32
        $sBytes &= " "
    Next

    $nTimer = TimerInit()

    ; Send the 32 bytes to the server
    $nSent = TCPSend($nSock, $sBytes)
    if (@error or $nSent <= 0) then Return -1

    $nTimerDiff = TimerDiff($nTimer)

    TCPCloseSocket($nSock)

    Return $nTimerDiff
EndFunc

If $Server1 = -1 Then
    $SServer1 = "close"
Else
    $SServer1 = "open"
EndIf

If $Server2 = -1 Then
    $SServer2 = "close"
Else
    $SServer2 = "open"
EndIf

MsgBox(-1, "Servers Status", "Server 1: " & $SServer1 & @CRLF & "Server 2: " & $SServer2)
Edited by Ticki84

Share this post


Link to post
Share on other sites
mlukac89

Here i found this on this forum, try run 1 by 1

UDP

#include<GUIConstantsEx.au3>
$sock = ""
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("UDP", 328, 447, 193, 125)
$IPin = GUICtrlCreateInput("", 50, 0, 121, 21)
GUICtrlCreateLabel("IP/Host:", 0, 0, 50, 17)
$History = GUICtrlCreateEdit("", 2, 82, 321, 361)
$Go = GUICtrlCreateCheckbox("Start", 250, -2, 75, 25, 0)
GUICtrlSetBkColor(-1, 0x009933)
GUICtrlCreateLabel("Port", 178, 2, 23, 17)
$Portin = GUICtrlCreateInput("", 204, 0, 45, 21)
GUICtrlCreateLabel("History", 2, 62, 36, 17)
$Gstatus = GUICtrlCreateLabel("", 74, 30, 196, 41)
GUICtrlSetFont(-1, 26, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=
UDPStartup()

While 1
    $msgb = GUIGetMsg()
    Switch $msgb
        Case $GUI_EVENT_CLOSE
            UDPCloseSocket($sock)
            UDPShutdown()
            Exit

        Case BitAND(GUICtrlRead($go),$GUI_CHECKED)

        Case BitAND(GUICtrlRead($go),$GUI_UNCHECKED)
            $ip = GUICtrlRead($IPin, 1)
            $port = GUICtrlRead($Portin, 1)
            If $ip = "" Or $port = "" Then
            Else
                $ip = TCPNameToIP($ip)
                Sleep(1000)
                $start = TimerInit()
                $sock = UDPBind($ip, $port)
                If @error = 1 Then
                    MsgBox(0, "Error", "Invalid IP entered!")
                    GUICtrlSetState($go, $GUI_UNCHECKED)
                ElseIf @error = 2 Then
                    MsgBox(0, "Error", "Invalid/closed Port entered!")
                    GUICtrlSetState($go, $GUI_UNCHECKED)
                ElseIf $sock = -1 Then
                    GUICtrlSetData($Gstatus, "OFFLINE!")
                    GUICtrlSetFont($gstatus, 26)
                    GUICtrlSetColor($gstatus, 0xFF0093)
                    GUICtrlSetData($History, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] OFFLINE, "&$ip&":"&$port & @CRLF, GUICtrlRead($History))
                Else
                    $Stop = Round(TimerDiff($start), 2)
                    GUICtrlSetData($Gstatus, "ONLINE!")
                    GUICtrlSetFont($gstatus, 26)
                    GUICtrlSetColor($gstatus, 0x009933)
                    GUICtrlSetData($History, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] ONLINE, "&$ip&":"&$port&" ms:" & $Stop & @CRLF, GUICtrlRead($History))
                    UDPCloseSocket($sock)
                EndIf
            EndIf
    EndSwitch
WEnd

And TCP

#include<GUIConstantsEx.au3>
$sock = ""
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("TCP", 328, 447, 193, 125)
$IPin = GUICtrlCreateInput("", 50, 0, 121, 21)
GUICtrlCreateLabel("IP/Host:", 0, 0, 50, 17)
$History = GUICtrlCreateEdit("", 2, 82, 321, 361)
$Go = GUICtrlCreateCheckbox("Start", 250, -2, 75, 25, 0)
GUICtrlSetBkColor(-1, 0x009933)
GUICtrlCreateLabel("Port", 178, 2, 23, 17)
$Portin = GUICtrlCreateInput("", 204, 0, 45, 21)
GUICtrlCreateLabel("History", 2, 62, 36, 17)
$Gstatus = GUICtrlCreateLabel("", 74, 30, 196, 41)
GUICtrlSetFont(-1, 26, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=
TCPStartup()

While 1
    $msgb = GUIGetMsg()
    Switch $msgb
        Case $GUI_EVENT_CLOSE
            TCPCloseSocket($sock)
            TCPShutdown()
            Exit

        Case BitAND(GUICtrlRead($go),$GUI_CHECKED)

        Case BitAND(GUICtrlRead($go),$GUI_UNCHECKED)
            $ip = GUICtrlRead($IPin, 1)
            $port = GUICtrlRead($Portin, 1)
            If $ip = "" Or $port = "" Then
            Else
                $ip = TCPNameToIP($ip)
                Sleep(1000)
                $start = TimerInit()
                $sock = TCPConnect($ip, $port)
                If @error = 1 Then
                    MsgBox(0, "Error", "Invalid IP entered!")
                    GUICtrlSetState($go, $GUI_UNCHECKED)
                ElseIf @error = 2 Then
                    MsgBox(0, "Error", "Invalid/closed Port entered!")
                    GUICtrlSetState($go, $GUI_UNCHECKED)
                ElseIf $sock = -1 Then
                    GUICtrlSetData($Gstatus, "OFFLINE!")
                    GUICtrlSetFont($gstatus, 26)
                    GUICtrlSetColor($gstatus, 0xFF0093)
                    GUICtrlSetData($History, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] OFFLINE, "&$ip&":"&$port & @CRLF, GUICtrlRead($History))
                Else
                    $Stop = Round(TimerDiff($start), 2)
                    GUICtrlSetData($Gstatus, "ONLINE!")
                    GUICtrlSetFont($gstatus, 26)
                    GUICtrlSetColor($gstatus, 0x009933)
                    GUICtrlSetData($History, "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] ONLINE, "&$ip&":"&$port&" ms:" & $Stop & @CRLF, GUICtrlRead($History))
                    TCPCloseSocket($sock)
                EndIf
            EndIf
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites
Ticki84

I only need TCP, but is there anyway to not do it 1 by 1 because i have 9 servers ports to test.

Share this post


Link to post
Share on other sites
mlukac89

It can just script to be adapted to your needs and you need better GUI. It dont be complex to be effective

something like this

http://prntscr.com/2yp1ca

Edited by mlukac89

Share this post


Link to post
Share on other sites
Ticki84

I've done something like that and i readapt the script and now it's working fine. Really thanks for all your help guys.

Share this post


Link to post
Share on other sites
mlukac89

Glad to help you :)

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  

×