Jump to content

Ping a specific port


Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 5 years later...

I made some changes to the TCP source code of @mlukac89 posted above to monitor multiple ports (spacebar delimiter)

To monitor multiple ports, just input e.g 80 800 8000 into inputbox

#include <GUIConstantsEx.au3>
#include <StringConstants.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()
;118.70.78.54:80 8000 3116 3389
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
                $aPort = StringSplit($port, " ", $STR_NOCOUNT)
                For $p In $aPort
                    PingPort($ip, $p)
                Next
            EndIf
    EndSwitch
WEnd


Func PingPort($ip, $port)
    $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
EndFunc   ;==>PingPort

Result is like this

image.png.1829e0201e347de2035456fdac394180.png

Link to comment
Share on other sites

  • 3 years later...

You could do this a few ways--for example use an external file (e.g. INI) with the details, or simplest would be just to embed the inform into the script.  With that route you can just prepopulate the controls with the IP and ports information.

Spoiler
;untested
$IPin = GUICtrlCreateInput("201.10.30.88", 50, 0, 121, 21)

$Portin = GUICtrlCreateInput("50000 50001 50002", 204, 0, 45, 21)

 

 

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