Page 1 of 1
Check if connection to server and do something
Posted: 20 Feb 2018, 07:03
by MyGiG
Hello,
i'm having trouble writing code that will do the following:
infinite loop. Every 1 minute check if the computer is online (try to ping a server or something), if the machine is online, do nothing, else try again in a minute (do nothing basically), but if machine is offline for 5 minutes then shut it down.
What is going on is, I bought a UPS, that doesn't have a USB port, so the UPS can't shutdown my PC. So figured, if my connection is down then there is a 99% chance that my electricity isn't working either (my ISP is great).
been digging the forum and can't figure it out by myself
Can anybody help me? Buying virtual beer
Re: Check if connection to server and do something
Posted: 20 Feb 2018, 08:01
by BoBo
Would you mind to specify that UPS (brand/type/..)?
Re: Check if connection to server and do something
Posted: 20 Feb 2018, 10:01
by noname
Why not getting a USB Port card ?
Skan has provided the code for checking inernetconnection , tried it on win10 .The code is basic you need to adapt it to your needs
Code: Select all
#persistent
SetTimer, check_connection, 300000 ; 5min = 5*60*1000 ms
return
check_connection:
If ConnectedToInternet()
return
else
{
Msgbox, 4, WinInet.dll, OFFLINE! Shutdown? , 20 ;timeout 20 sec
IfMsgBox, yes
Shutdown, Code
else
exitapp ; to prevent repeating shutdown attemps
}
Return
; code from Skan https://autohotkey.com/board/topic/80587-how-to-find-internet-connection-status/
ConnectedToInternet(flag=0x40) {
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0)
}
Re: Check if connection to server and do something
Posted: 21 Feb 2018, 00:53
by MyGiG
My UPS is Tecnoware UPS ERA Plus 750. how could I install a USB card? thanks for the code, wil try it later, when i get home from work.
Re: Check if connection to server and do something
Posted: 21 Feb 2018, 02:00
by Masonjar13
InternetGetConnectedState doesn't actually check for connection, but validity of the connection state (LAN). For your case, this should work, considering if your router/AP has no power, there will be no LAN. To reliably check if you are connected to the internet, however, you should use Ping a server, either in the console (see
comspec), or you can use
InternetCheckConnection.
Re: Check if connection to server and do something
Posted: 21 Feb 2018, 02:31
by MyGiG
What do you mean with reliably? is the InternetGetConnectedState unreliable or did you mean something else?
Re: Check if connection to server and do something
Posted: 21 Feb 2018, 05:34
by noname
Masonjar13 has a valid point.But if your router is not powered through the UPS the check is enough.I would use this instead of pinging for just looking if power is off.
Actually i would install a USB hub or card ,it is only a few euros on amazon
Re: Check if connection to server and do something
Posted: 21 Feb 2018, 08:42
by MyGiG
Thanks for the reply. you misunderstood me, the UPS doesn't have a USB interface, so I can't connect it to my PC (which is basicaly my home server running 24/7), that is powered by the UPS. Thats why I need the program to shutdown my pc
Re: Check if connection to server and do something
Posted: 21 Feb 2018, 09:32
by BoBo
I don't get the idea why that UPS keeps the box alive but won't allow triggering it for a managed shutdown
Re: Check if connection to server and do something
Posted: 21 Feb 2018, 11:06
by noname
My bad MyGiG for not reading your post properly.I am so used to have UPS with usb that i assumed it was the PC lacking a spare one
Greetings
Re: Check if connection to server and do something
Posted: 22 Feb 2018, 01:15
by MyGiG
Yeah, I didn't even know, they come without a USB interface :/ learned the hard way. Anyway thank you all for the replys!