Check if connection to server and do something

Get help with using AutoHotkey and its commands and hotkeys
MyGiG

Check if connection to server and do something

20 Feb 2018, 07:03

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 :P :beer:
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Check if connection to server and do something

20 Feb 2018, 08:01

Would you mind to specify that UPS (brand/type/..)?
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Check if connection to server and do something

20 Feb 2018, 10:01

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) 
}
MyGiG

Re: Check if connection to server and do something

21 Feb 2018, 00:53

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.
User avatar
Masonjar13
Posts: 1405
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: Check if connection to server and do something

21 Feb 2018, 02:00

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

Re: Check if connection to server and do something

21 Feb 2018, 02:31

What do you mean with reliably? is the InternetGetConnectedState unreliable or did you mean something else?
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Check if connection to server and do something

21 Feb 2018, 05:34

Image


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 ;)
MyGiG

Re: Check if connection to server and do something

21 Feb 2018, 08:42

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 :)
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Check if connection to server and do something

21 Feb 2018, 09:32

I don't get the idea why that UPS keeps the box alive but won't allow triggering it for a managed shutdown :crazy:
User avatar
noname
Posts: 507
Joined: 19 Nov 2013, 09:15

Re: Check if connection to server and do something

21 Feb 2018, 11:06

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 :oops:
Greetings
MyGiG

Re: Check if connection to server and do something

22 Feb 2018, 01:15

Yeah, I didn't even know, they come without a USB interface :/ learned the hard way. Anyway thank you all for the replys!

Return to “Ask For Help”

Who is online

Users browsing this forum: gazmoz17, John, loek3000, Oldboy, swagfag, vvhitevvizard, wise_mike and 72 guests