Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

SimplePing - Successor of Ping()


  • Please log in to reply
8 replies to this topic
Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010

SimplePing

AHK library providing a variety of ICMP pinging-related functionality.

 

Examples
Ping a Google DNS server:
MsgBox % "Round trip time: " . RoundTripTime("8.8.8.8")
 
Ping two different Google DNS servers at the same time:
Addresses := ["8.8.8.8", "8.8.4.4"]
Value := ""
For Index, PingTime In RoundTripTimeList(Addresses)
    Value .= "Server: "List[Index] . ", Round trip time: " . PingTime . "ms`n"
MsgBox %Value%
Send an ICMP request to a Google DNS server:
MsgBox % "Round trip time: " . Ping("8.8.8.8") . "ms"
 
Ping example.com with some data and show the response (the SubStr causes the result to be interpreted as a string):
MsgBox % "Round trip time: " . Ping("192.0.43.10",800,"Hello!",6,Value,Length) . "ms, Response: " . SubStr(Value,1)
 
Overview
All functions throw exceptions on failure.
RTT := Ping(Address,Timeout = 800,ByRef Data = "",Length = 0,ByRef Result = "",ByRef ResultLength = 0)
Pings an address and waits for a response.
Useful for sending custom pings supporting messages and responses.
 
RTT := PingAsync(Address,Timeout = 800,ByRef Data = "",Length = 0,ByRef Result = "",ByRef ResultLength = 0)
Works exactly the same as Ping(), but uses an asynchronous event-based method.
Intended only as a technology demo, avoid using as it may change or be removed in the future.
RTT := RoundTripTime(Address,Timeout = 800)
Determines the round trip time (sometimes called the "ping") from the local machine to the address.
Useful for pinging a server to determine latency.
RTT := RoundTripTimeList(AddressList,Timeout = 800)
Determines the round trip time (sometimes called the "ping") from the local machine to a list of 64 or fewer addresses.
Useful for quickly pinging an entire server list to find the amount of latency in the connection to each.
 
Parameters
RTT:          The round trip time in milliseconds (e.g., 76). If unknown or timed out, this value is -1.
Address:      IPv4 address as a string in dotted number format (e.g., "127.0.0.1").
AddressList:  Array of IPv4 addresses as strings in dotted number format (e.g., ["127.0.0.1", "8.8.4.4"]).
Timeout:      How long the function should wait, in milliseconds, before giving an error (e.g., 400).
Data:         The data to send with the ping (e.g., "Hello"). Can be binary data as well.
Length:       The length of the data to send with the ping (e.g., 5).
Result:       The data in the response to the ping (e.g., "Salutations"). Can be binary data as well.
ResultLength: The length of the data in the response to the ping (e.g., 11).

 

Download

Script

Gist



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
uuuusefull..

any updates? I cant ping(IP:Port)
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010

Pinging works on a lower network layer, where there is no such thing as a port. You can simply remove the port, and it will work correctly.

 

Also, this library has received a massive update, and is now known as SimplePing. See the first post for more info.



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
how can programs like xfire ping same server with different ports with different ping results?
eg.
100.200.300.400:10001 - Ping 32
100.200.300.400:10002 - Ping 30
100.200.300.400:10006 - Ping 36
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

ninix
  • Members
  • 19 posts
  • Last active: Mar 15 2014 10:29 AM
  • Joined: 28 Apr 2012

Hi,

 

Thanks for the functions!

 

But there seems to be a problem...

It ignores the Timeout value!

No mather what I use it has no effect.

 

I use AHK 1.1.14.03 on WinXP

Cheers.



ninix
  • Members
  • 19 posts
  • Last active: Mar 15 2014 10:29 AM
  • Joined: 28 Apr 2012

I found some code on how to get text from a cmd window and I made another function to do ping using ping.exe from cmd, but without using a text file (read/write output to it)

#NoEnv
#NoTrayIcon
#WinActivateForce
#SingleInstance force
SendMode, Input
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2
DetectHiddenWindows, On
DetectHiddenText, On
SetKeyDelay, 0
SetControlDelay -1

If(Ping("8.8.8.8"))
	{
	 msgbox,,, Ping ok
	}
Else
	{
	 msgbox,,, Ping fail
	}

;*******************************************[ Ping Function ]***************************
Ping(IP, Timeout = 1500, Counts = 2)
	{
	 global
	 
	 ;If your windows is not in English please modify this!!!
	 CMD_RGX       = Reply from %IP%: bytes=32 time
	 CMD_END       = #########
	 ConWinWidth   = 45  ;Maximum line length (CMD line length)
	 ConWinHeight  = 15  ;How many lines to read from CMD output
	 Buff_Ext_Text = 0

	 Run, %COMSPEC% /C MODE CON: cols=75 lines=25& PING -n %Counts% -w %Timeout% %IP% &ECHO %CMD_END%&PAUSE >NUL ,,Hide, PID_CMD
	 WinWait, ahk_pid %PID_CMD%,, 15
	 If ErrorLevel
		{
		 Return -1
		}

	 If(!AttachConsole(PID_CMD))
		{
		 Process, Close, %PID_CMD%
		 Return -1
		}

	 Loop, 40 ;10 seconds...
		{
		 Buff_Ext_Text := GetConsoleText(ConWinWidth, ConWinHeight)
		 If(Buff_Ext_Text = 0)
			{
			 FreeConsole(PID_CMD)
			 Return -1
			}
		 If(InStr(Buff_Ext_Text, CMD_END))
			{
			 If(RegExMatch(Buff_Ext_Text, CMD_RGX))
				{
				 FreeConsole(PID_CMD)
				 Return 1 ;Ping ok...
				}
			 Else
				{
				 FreeConsole(PID_CMD)
				 Return 0 ;Ping fail...
				}
			}
		 Else
			{
			 Sleep, 250
			}
		}
	}
;*******************************************[ Hook to CMD Process ]***************************
AttachConsole(PID)
	{
	 global hConOut

	 If (!DllCall("AttachConsole", "uint", PID))
		{
		 MsgBox,,, AttachConsole failed - error %A_LastError%
		 Return 0
		}

	 hConOut:=DllCall("CreateFile","str","CONOUT$","uint",0xC0000000,"uint",7,"uint",0,"uint",3,"uint",0,"uint",0)
	 If (hConOut = -1)
		{
		 MsgBox,,, CreateFile failed - error %A_LastError%
		 Return 0
		}

	 Return 1
	}
;*******************************************[ Free console and kill CMD Process ]***********************
FreeConsole(PID)
	{
	 global hConOut

	 DllCall("FreeConsole")
	 Process, Close, %PID%

	 hConOut :=
	}
;*******************************************[ Extract CMD Window Output ]********************************
GetConsoleText(ConWinWidth, ConWinHeight)
	{
	 global hConOut

	 VarSetCapacity(info, 24, 0)
	 If (!DllCall("GetConsoleScreenBufferInfo","uint",hConOut,"uint",&info))
		{
		 MsgBox,,, GetConsoleScreenBufferInfo failed - error %A_LastError%
		 Return 0
		}

	 VarSetCapacity(buf, ConWinWidth*ConWinHeight*4, 0)
	 If (!DllCall("ReadConsoleOutput","uint",hConOut,"uint",&buf,"uint",ConWinWidth|ConWinHeight<<16,"uint",0,"uint",&info+10))
		{
		 MsgBox,,, ReadConsoleOutput failed - error %A_LastError%
		 Return 0
		}

	 VarSetCapacity(Temp_Buffer, ConWinWidth*ConWinHeight)
	 Loop % ConWinWidth*ConWinHeight ;%
		{
		 Temp_Buffer .= Chr(NumGet(buf, 4*(A_Index-1), "Char"))
		}

	 Return Temp_Buffer
	}



badon
  • Members
  • 4 posts
  • Last active: Apr 09 2014 06:36 AM
  • Joined: 21 Jan 2013

I tried the SimplePing examples, and none of them worked. They all gave me -1ms, or crashed with errors. The script by ninix worked on the first try, but it's not quite a library yet. Advice?



badon
  • Members
  • 4 posts
  • Last active: Apr 09 2014 06:36 AM
  • Joined: 21 Jan 2013

FYI, this version of Uberi's code worked for me:

 

http://ahkscript.org...topic.php?t=552



badon
  • Members
  • 4 posts
  • Last active: Apr 09 2014 06:36 AM
  • Joined: 21 Jan 2013

 

I found some code on how to get text from a cmd window and I made another function to do ping using ping.exe from cmd, but without using a text file (read/write output to it)

 

Can you share where you found that code?