Random internet drops... looking for ways to log when internet goes down.

Associate
Joined
9 Aug 2009
Posts
1,517
Location
Internet
I keep getting random internet drops during the night and day which doesn't help when hosting servers and people can't connect to them so i am looking for ways or getting a program that can log to a file when the internet is active and when it goes down, help is very much appreciated.
 
Man of Honour
Joined
19 Oct 2002
Posts
29,524
Location
Surrey
I've been dabbling with Python tonight so I wrote a Python script to do this. To run it:

1) Install Python if you don't already have it (https://www.python.org/downloads/)
2) Copy the below script to a text file and save it somewhere as networkcheck.py
3) To run it, open a command prompt, change the directory to wherever you saved networkcheck.py and the type py networkcheck.py

It will check the state of the network connection evey 60 seconds (you can change this period to a different amount by changing the waittime variable in the script). It will log the results to c:\logs\networkcheck.txt. You'll need to either create the c:\logs directory or change the script to write to a different directory. Just CTRL-C to stop it.

Code:
import datetime
import os
import time

filename = "c:\\logs\\networkcheck.txt"   # Output log file name
hostname = "www.google.co.uk"             # Host name or ip address to ping
waittime = 60                             # Waits 60 seconds between checks

lastresponse = 1;
response = 0;

file = open(filename, "w")

while True:

    response = os.system("ping -n 1 " + hostname + " > nul")

    if response != lastresponse:

        if response == 0:
            status = "Connected"
        else:
            status = "No network connection"

        logmessage = "[" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "] " + status + "\n"
        file.write(logmessage)
        file.flush()
        lastresponse = response
        time.sleep(waittime)



It will write a "Connected" or "No network connection" message every time the network connection is lost or reconnects. It won't log messages if the status stays the same (e.g. it won' continually log messages every minute - it will only log them if the status changes).


Sample output:

Code:
[2017-12-18 00:32:36] Connected
[2017-12-18 00:35:36] No network connection
[2017-12-18 00:36:36] Connected
 
Soldato
Joined
6 Mar 2008
Posts
10,078
Location
Stoke area
I've been dabbling with Python tonight so I wrote a Python script to do this. To run it:

1) Install Python if you don't already have it (https://www.python.org/downloads/)
2) Copy the below script to a text file and save it somewhere as networkcheck.py
3) To run it, open a command prompt, change the directory to wherever you saved networkcheck.py and the type py networkcheck.py

It will check the state of the network connection evey 60 seconds (you can change this period to a different amount by changing the waittime variable in the script). It will log the results to c:\logs\networkcheck.txt. You'll need to either create the c:\logs directory or change the script to write to a different directory. Just CTRL-C to stop it.

Code:
import datetime
import os
import time

filename = "c:\\logs\\networkcheck.txt"   # Output log file name
hostname = "www.google.co.uk"             # Host name or ip address to ping
waittime = 60                             # Waits 60 seconds between checks

lastresponse = 1;
response = 0;

file = open(filename, "w")

while True:

    response = os.system("ping -n 1 " + hostname + " > nul")

    if response != lastresponse:

        if response == 0:
            status = "Connected"
        else:
            status = "No network connection"

        logmessage = "[" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "] " + status + "\n"
        file.write(logmessage)
        file.flush()
        lastresponse = response
        time.sleep(waittime)



It will write a "Connected" or "No network connection" message every time the network connection is lost or reconnects. It won't log messages if the status stays the same (e.g. it won' continually log messages every minute - it will only log them if the status changes).


Sample output:

Code:
[2017-12-18 00:32:36] Connected
[2017-12-18 00:35:36] No network connection
[2017-12-18 00:36:36] Connected

Or you could create an exe for him so he doesn't have to worry about installing python, setting it up etc :)
 
Joined
1 Oct 2006
Posts
13,900
I'd also run multiple pings out, see if you can see what is failing exactly. Is your line dropping, modem rebooting or something else funky going on. By pinging an internet address (say Google's DNS servers) and your inside gateway IP you'd have an idea of which is failing.

Also, what router do you have? If it's aftermarket you may have logs on there that'll give you an idea of what's going on.
 
Man of Honour
Joined
19 Oct 2002
Posts
29,524
Location
Surrey
Or you could create an exe for him so he doesn't have to worry about installing python, setting it up etc :)
Well true. But as I wrote it at midnight I fancied some sleep rather than either convert it to an exe or rewite it in a compiled language. Python is a couple of clicks to install and it also means he can easily change the destination host, filename and sleep time.

But by all means convert it to an exe for him with py2exe if you have a few spare mins.
 
Soldato
Joined
1 Mar 2010
Posts
21,916
Or you could create an exe for him so he doesn't have to worry about installing python, setting it up etc
it's the old teach a man to fish addage ?

I am still not unconvinced that a ping-out should be complimented by an external service checking your access eg. other folks may use different dns servers. so ping-out measures may not be reciprocated, or your http server is unable to respond.
 
Back
Top Bottom