Python Script to Monitor Network Connection and saving into Log File
In this article, we are going to see how to monitor the network connection and save the log file in Python.
The basic ideology of this script is to give real-time information about if the system the script is being run on is connected to an internet connection or not, and save that information into a log file simultaneously, keeping records of when the system was connected to the internet when it was disconnected and the time duration it was disconnected for.
This script is made using the socket library in Python, which in this program is used to send or receive packets on a network.
Starting simply by creating a log file in the current working directory to store the internet connectivity status.
Using this function the script will try connecting to the defined server, to check if the system has a live internet connection. This task will be done using exception handling in python (try, except, else).
- The system will try pinging a specific server(PORT at an IP)
- If the machine fails to connect, EXCEPT statement will be executed
- Else the connection will be closed after the system is successfully connected to the server
Functions Used in above program
- socket.setdefaulttimeout() : It is an inbuilt socket library function in python. by setting default timeout as 3 seconds, we specify if we do not get any response from the server for more than 3 seconds, the connection is not made.
- socket.socket(socket.AF_INET, socket.SOCK_STREAM): socket.socket() is used to define parameters to connect two nodes in a network, i.e. your system to a particular PORT at a particular IP so that they can communicate to each other.
- AF_INET is an address family used to accept IP of address type v4 as a parameter to which the defined socket will communicate
- SOCK_STREAM is a connection-based protocol, in this program using TCP(transmission control protocol) is used to accept a port no. as a parameter
Unavailability time is the time duration for which the internet connection was unavailable. is calculated using downtime(stop) time when the internet connection got lost and uptime(start) time when the internet connection got restored
This function will only be executed once, i.e. at the beginning of the script to check if the system is already connected to an internet connection or not, and write that into the log file. ping() function is called
- If ping returns true(machine is connected to the internet), the script will print “CONNECTION ACQUIRED” and write the same into the log file.
- If ping returns false(the system is not connected to the internet), the script will print “CONNECTION NOT ACQUIRED” and write the same into the log file.
The main function, where all the user-defined programs will be executed and the live internet status will be written into a log file.
- First, first_check() will be executed (this function will be executed only one time, i.e. at the beginning of the script)
- If true(connection acquired): monitoring will start
- If false(connection not acquired): else statement will be executed having infinite while loop to check for the internet connection using ping() function
- If ping will return false, the loop will be executed after every second until the ping() function returns true
- If ping will return true(connection acquired), the loop will break, and monitoring will be started
- Second, to monitor the network connection, the first while statement will be executed, which is an infinite loop
- If ping returns true, the loop will be executed after every 5 seconds until it returns false
- If ping returns false, downtime will be printed, and the loop will execute after every second until an internet connection is restored
- After internet connection is restored, uptime and unavailability time will be printed, and iteration will get back to the start of the loop
Below is the implementation:
CONNECTION ACQUIRED connection acquired at: 2021-09-16 15:03:18 monitoring started at: 2021-09-16 15:03:18 disconnected at: 2021-09-16 15:03:49 connected again: 2021-09-16 15:03:50 connection was unavailable for: 0:00:01