Ping Function like autoitscript
Ping Function like autoitscript
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
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
- Blackholyman
- Posts: 1293
- Joined: 29 Sep 2013, 22:57
- Location: Denmark
- Contact:
Re: Ping Function like autoitscript
This maybe be of use
Hope it helps
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
}
Courses on AutoHotkey
My Autohotkey Blog
Re: Ping Function like autoitscript
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://
Re: Ping Function like autoitscript
Use WMI?
Hope this helps...
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.
}
Re: Ping Function like autoitscript
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?
I tried %A% and '%A%' , just A, $A and %A
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.
}
Re: Ping Function like autoitscript
("Select * From Win32_PingStatus where Address =" A)
Also, nice name
Also, nice name
Re: Ping Function like autoitscript
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%
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.
Re: Ping Function like autoitscript
Fixing everything for you, hang tight. :p
Re: Ping Function like autoitscript
Ummm... if, for example, your router has an IP address of 10.20.24.1 then just amend the script as follows:
Hope this helps...
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.
}
Last edited by RickC on 14 Nov 2014, 12:09, edited 1 time in total.
Re: Ping Function like autoitscript
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, 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.
Re: Ping Function like autoitscript
Try amending this to your liking using 'MyVar' as your input variable.
Hope this helps...
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
Re: Ping Function like autoitscript
You don't need half of the code that's there. I'm still working on a script for you BankSea...
- Blackholyman
- Posts: 1293
- Joined: 29 Sep 2013, 22:57
- Location: Denmark
- Contact:
Re: Ping Function like autoitscript
Do like this
Code: Select all
("Select * From Win32_PingStatus where Address = '" MyVar "'")
Courses on AutoHotkey
My Autohotkey Blog
Re: Ping Function like autoitscript
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.
Re: Ping Function like autoitscript
@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.
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.
Re: Ping Function like autoitscript
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).
Edit: also changed the sleep a little (also untested).
Re: Ping Function like autoitscript
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.
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.
Re: Ping Function like autoitscript
Modified version of uberi's ping()
https://github.com/ahkscript/ASPDM/blob ... b/Ping.ahk
https://github.com/ahkscript/ASPDM/blob ... b/Ping.ahk
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]