Ping Function like autoitscript

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BankSea
Posts: 10
Joined: 20 Oct 2014, 12:26

Ping Function like autoitscript

12 Nov 2014, 17:49

Hey guys!

I've seen a couple PING applications and scripts created but I cannot find something as simple as this ping function I found for autoit https://www.autoitscript.com/autoit3/do ... s/Ping.htm


Is there a command where I can ping an address and it will rerun a value if online and another if offline?

I wouldn't need every error code, but success or failure.

Thanks!

I've been working on an app for 3 months and I still feel like a noob :P
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Ping Function like autoitscript

12 Nov 2014, 18:28

This maybe be of use

Code: Select all

if (var := Ping_like("http://www.google.com"))
	msgbox % "time: " var "ms"
else
	msgbox % "Error: " Errorlevel
return

Ping_like(Url)
{
	WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	start_time := A_TickCount
	try 
		WebRequest.Open("TRACE", url, false)
	catch error
	{
		Errorlevel := error.Message
		return false
	}
	WebRequest.send()
	ping_time := A_TickCount - start_time
	return ping_time
}
Hope it helps
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
BankSea
Posts: 10
Joined: 20 Oct 2014, 12:26

Re: Ping Function like autoitscript

14 Nov 2014, 09:38

Thanks for the response Blackholyman :) Unfortinatly, that script did not work for what I wanted, I think it's because i'm trying to ping a device. not a website so it won't be http://
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Ping Function like autoitscript

14 Nov 2014, 09:46

Use WMI?

Code: Select all

strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")

colPings := objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = 'www.google.com'")._NewEnum ;or ip address like 192.168.1.1

While colPings[objStatus]
{
    If (objStatus.StatusCode="" or objStatus.StatusCode<>0)
        MsgBox Remote URL or IP did not respond.
    Else
        MsgBox Remote URL or IP responded.
}
Hope this helps...
BankSea
Posts: 10
Joined: 20 Oct 2014, 12:26

Re: Ping Function like autoitscript

14 Nov 2014, 10:25

That worked but the URL being pinged will be variable/user inputed, do I have to format the variable differently in line 8 since it's ComObj?

Code: Select all

inputbox, A

Ping:

strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")

colPings := objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = A ")._NewEnum ;or ip address like 192.168.1.1

While colPings[objStatus]
{
    If (objStatus.StatusCode="" or objStatus.StatusCode<>0)
        MsgBox Remote URL or IP did not respond.
    Else
        MsgBox Remote URL or IP responded.
}
I tried %A% and '%A%' , just A, $A and %A
Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: Ping Function like autoitscript

14 Nov 2014, 10:37

("Select * From Win32_PingStatus where Address =" A)

Also, nice name :P
BankSea
Posts: 10
Joined: 20 Oct 2014, 12:26

Re: Ping Function like autoitscript

14 Nov 2014, 11:24

Code: Select all

inputbox, A


strComputer := "."
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")

colPings := objWMIService.ExecQuery("Select * From Win32_PingStatus where Address =" A)._NewEnum ;or ip address like 192.168.1.1

While colPings[objStatus]
{
    If (objStatus.StatusCode="" or objStatus.StatusCode<>0)
        MsgBox Remote URL or IP did not respond.
    Else
        MsgBox Remote URL or IP responded.
}


msgbox,,, %a%
My Name :P Thanks, it's just a spin-off of the street artist Banksy.

BTW, I don't know if i'm using the wrong version of something but it's still not working :( i'm on AHK Version v1.1.15.00

it will ask for the input and quickly goes to the last line so I don't think it's even pinging since there isn't a time out wait period in the script.
Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: Ping Function like autoitscript

14 Nov 2014, 11:50

Fixing everything for you, hang tight. :p
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Ping Function like autoitscript

14 Nov 2014, 11:58

Ummm... if, for example, your router has an IP address of 10.20.24.1 then just amend the script as follows:

Code: Select all

strComputer := "."
strDest := 10.20.24.1
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")

colPings := objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = '%strDest%'")._NewEnum ;or ip address like 192.168.1.1

While colPings[objStatus]
{
    If (objStatus.StatusCode="" or objStatus.StatusCode<>0)
        MsgBox Remote URL or IP did not respond.
    Else
        MsgBox Remote URL or IP responded.
}
Hope this helps...
Last edited by RickC on 14 Nov 2014, 12:09, edited 1 time in total.
BankSea
Posts: 10
Joined: 20 Oct 2014, 12:26

Re: Ping Function like autoitscript

14 Nov 2014, 12:09

Thanks Bkid

RickC, it works when I enter the IP, but the IP will be variable based on what the user inputs in an input box, so instead of
("Select * From Win32_PingStatus where Address = '10.20.24.1'")
I would need
("Select * From Win32_PingStatus where Address = '%MyVar%'")
but that doesnt seem to work.
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Ping Function like autoitscript

14 Nov 2014, 12:24

Try amending this to your liking using 'MyVar' as your input variable.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory
#SingleInstance force
Gui, Add, Text, x12 y10 w310 h30, Please enter the remote IP address or computername below. ; Ask for IP
Gui, Add, Button, x230 y40 w60 h20, OK ; Add OK button
Gui, Add, Edit, x12 y40 w200 h20 vMyVar, ;Add edit box to input IP address of remote PC
Gui, Show, w320 h80, My Availability Checker ; Show the dialog
GuiControl, Focus, MyVar ; Put the focus into the edit box
Return

~Enter:: ; Define ENTER as hotkey
~NumpadEnter:: ; Define ENTER on NumPad as additional hotkey

ButtonOK:
Gui, Submit  ; Save the input from the user to each control's associated variable

strComputer := "." ; THIS computer
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\" . strComputer . "\root\cimv2")

colPings := objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = '%MyVar%'")._NewEnum ;or ip address like 192.168.1.1

While colPings[objStatus]
{
    If (objStatus.StatusCode="" or objStatus.StatusCode<>0)
        MsgBox Remote URL or IP did not respond.
    Else
        MsgBox Remote URL or IP responded.
}

GuiClose: ; Close the GUI

ExitApp ; Exit the app
Hope this helps...
Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: Ping Function like autoitscript

14 Nov 2014, 12:25

You don't need half of the code that's there. I'm still working on a script for you BankSea...
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Ping Function like autoitscript

14 Nov 2014, 12:27

Do like this

Code: Select all

("Select * From Win32_PingStatus where Address = '" MyVar "'")
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: Ping Function like autoitscript

14 Nov 2014, 12:29

try this:

Code: Select all

#SingleInstance, Force

Gui, Add, Text,, Ping Address:
Gui, Add, Edit, x+2 yp-3 w200 vAddress
Gui, Add, Button, gGo x10 w267, Go
Gui, Add, Edit, x10 y+5 w267 h300 -vscroll ReadOnly vStatusBox
Gui, Show,, Pinger
Return

Go:
Gui, Submit, NoHide
GuiControl,, StatusBox, % "Pinging " Address " with 32 bytes of data:`n"
Loop, 4 {
	For Ping in (ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PingStatus where Address ='" Address "'")) {
	Gui, Submit, NoHide
	If(!Ping.ResponseTime)
		PingNext := StatusBox "Request Timed Out"
	Else
	PingNext := StatusBox "Reply from " Ping.Address ": bytes=" Ping.ReplySize " time " Ping.ResponseTime "ms TTL=" Ping.ResponseTimeToLive
	GuiControl,, StatusBox, % PingNext "`n"
	}
	If(Ping.ResponseTime > 1000)
		Sleep, % Ping.ResponseTime
	Else
		Sleep, 1000
}
Return
Last edited by Bkid on 11 Apr 2015, 08:53, edited 3 times in total.
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Ping Function like autoitscript

14 Nov 2014, 12:33

@Bkid - You're quite right. The code I suggested can be implemented as a much shorter function.

However, I was trying to show BankSea a documented example of what he/she was trying to achieve so he/she could perhaps learn what was going on behind the scenes instead of having a solution just presented au complet. :)
Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: Ping Function like autoitscript

14 Nov 2014, 12:53

Yeah, I understand. I added a quick and dirty (and untested) "timed out" section in there. It doesn't take into account the multiple other errors that could occur, though, but I'm a little too lazy for that right now. :p

Edit: also changed the sleep a little (also untested).
BankSea
Posts: 10
Joined: 20 Oct 2014, 12:26

Re: Ping Function like autoitscript

14 Nov 2014, 13:56

Thanks a lot for the help guys!

Blackholyman, that's what the script needed, thanks! quotes are weird.
Bkid, I didn't need the gui but that is really cool. Bookmarking/Saving for later, I will however use the script, I just need it to tell me if it's online or offline but I've figured it out from what you made.
RickC, Thanks, I'm gonna use some of that in my gui. It's more than just and inputbox. :P

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, mikeyww and 377 guests