Use python to call the ping command and record the ping result in the log to monitor network connectivity.
Code:
[Python]
From time import *
From subprocess import *
Webf = open ("webs.txt", "r ")
Webs = []
For w in webf:
Webs. append (w. strip ())
Def logAppend (log, info ):
Inttime = time ()
Structtime = localtime (inttime)
Strtime = strftime ("% Y-% m-% d, % H: % M: % S", structtime)
Print "at", strtime
Log. write ("========================" + strtime + "====================== ===\ n ")
Log. write (info)
Log. write ("\ n ")
Print "append info to file:", log. name
Print info
Def netCheck ():
While True:
For url in webs:
P = Popen (["ping.exe", url],
Stdin = PIPE, stdout = PIPE, stderr = PIPE,
Shell = True)
Out = p. stdout. read ()
Log = open ("log \" + url + ". log", "")
LogAppend (log, out)
Log. close ()
Sleep (0.01)
Print "waiting ..."
Sleep (60*15) # sleep for 15 min. 60*15
Return
Def main ():
"""
The main function
"""
Print "start ..."
NetCheck ()
Print "end ."
If _ name _ = "_ main __":
Main ()
From time import *
From subprocess import *
Webf = open ("webs.txt", "r ")
Webs = []
For w in webf:
Webs. append (w. strip ())
Def logAppend (log, info ):
Inttime = time ()
Structtime = localtime (inttime)
Strtime = strftime ("% Y-% m-% d, % H: % M: % S", structtime)
Print "at", strtime
Log. write ("========================" + strtime + "====================== ===\ n ")
Log. write (info)
Log. write ("\ n ")
Print "append info to file:", log. name
Print info
Def netCheck ():
While True:
For url in webs:
P = Popen (["ping.exe", url],
Stdin = PIPE, stdout = PIPE, stderr = PIPE,
Shell = True)
Out = p. stdout. read ()
Log = open ("log \" + url + ". log", "")
LogAppend (log, out)
Log. close ()
Sleep (0.01)
Print "waiting ..."
Sleep (60*15) # sleep for 15 min. 60*15
Return
Def main ():
"""
The main function
"""
Print "start ..."
NetCheck ()
Print "end ."
If _ name _ = "_ main __":
Main ()
Note:
For example, www.baidu.com.
Create a folder named log in the current directory.
About the time module:
[Python]
Inttime = time () # obtain the decimal form of the current time: 1366356992.617
Structtime = localtime (inttime) ### convert to local time,
# The returned result is: time. struct_time (tm_year = 2013, tm_mon = 4, tm_mday = 19,
Inttime = time () # obtain the decimal form of the current time: 1366356992.617
Structtime = localtime (inttime) ### convert to local time,
# The returned result is: time. struct_time (tm_year = 2013, tm_mon = 4, tm_mday = 19, [python] view plaincopyprint?
Tm_hour = 15, tm_min = 36, tm_sec = 32, tm_wday = 4, tm_yday = 109, tm_isdst = 0)
# This is not pleasing to the eye and continues formatting and conversion:
Strtime = strftime ("% Y-% m-% d, % H: % M: % S", structtime)
Tm_hour = 15, tm_min = 36, tm_sec = 32, tm_wday = 4, tm_yday = 109, tm_isdst = 0)
# This is not pleasing to the eye and continues formatting and conversion:
Strtime = strftime ("% Y-% m-% d, % H: % M: % S", structtime) [python] view plaincopyprint?
# The returned result is the string in the desired format: 2013-04-19,15: 36: 32
# The returned result is the string in the desired format: 2013-04-19,15: 36: 32
Other parameter types:
Strftime (format [, tuple])-> string
Returns the specified struct_time (current time by default) according to the specified formatted string.
Time and date formatting symbols in python:
% Y two-digit year (00-99)
% Y indicates the four-digit year (000-9999)
% M month (01-12)
One day in % d month (0-31)
% H hour in 24-hour format (0-23)
% I 12-hour (01-12)
% M minutes (00 = 59)
% S seconds (00-59)
% A local simplified week name
% A local full week name
% B local simplified month name
% B local full month name
% C local Date and Time
% J one day in the year (001-366)
% P local equivalent of A. M. or P. M.
% U number of weeks in a year (00-53) Sunday is the start of the week
% W Week (0-6), Sunday is the beginning of the week
% W number of weeks in a year (00-53) Monday is the start of the week
% X local date Representation
% X Local Time Representation
% Z Current Time Zone name
% Itself