Problem
Recent server access to other people's servers, sometimes reported time-out errors, and sometimes normal access to other people's servers.
Ideas
The first guess is caused by network instability, but they do not collect when time-out, when the normal access to other people's server logs, network operation and maintenance of the students do not bird me (in fact, this life is the operation of the matter, a little careful plug, but think of Spider-Man quotes).
The greater the capacity, the greater the responsibility.
Write a Python script and then, in a Python script, use Telnet to connect to the other server's port, and then calculate the length of time before and after the connection.
Solve
ImportOSImportCSVImport TimeImportArgparseImportTelnetlib fromDatetimeImportdatetime#Testing remote Service port connection time-consuming#Python3 windows_telnet.py 192.168.10.21Parser=Argparse. Argumentparser () parser.add_argument ("IP", Type=str, help="IP") parser.add_argument ("Port", Type=str, help="Port") args=Parser.parse_args () TimeFormat="%y-%m-%d%h:%m:%s.%f"Startimetitle="Start Connection Time"Endtimetitle="End Connection Time"Differencetimetitle="Total Connection Time" whileTrue:startime=DateTime.Now () Startimeview=startime.strftime (TimeFormat)Print("start connection: {0}". Format (Startimeview)) TN=Telnetlib. Telnet (Args.ip, args.port) endTime=DateTime.Now () Endtimeview=endtime.strftime (TimeFormat)Print("Connection complete: {0}". Format (Endtimeview)) Tn.close ()Print("End of Connection") Differencetime= EndTime-StartimePrint("connection consumption: {0}". Format (differencetime)) Nowtime=DateTime.Now () csvfilename="{0}.csv". Format (Nowtime.strftime ("%y-%m-%d")) ifOs.path.exists (Csvfilename) is notTrue:with Open (Csvfilename,"W", newline="") as Csvfile:fieldnames=[Startimetitle, Endtimetitle, Differencetimetitle] writer= csv. Dictwriter (CSVFile, fieldnames=fieldnames) Writer.writeheader () with open (Csvfilename,"a", newline="") as Csvfile:fieldnames=[Startimetitle, Endtimetitle, Differencetimetitle] writer= csv. Dictwriter (CSVFile, fieldnames=fieldnames) Writer.writerow ({startimetitle:startimeview, Endtimetitle:endtimeview, Differencetimetitle:di Fferencetime}) Time.sleep (0.2)
Here are a few python points of knowledge:
- Gets the current time, calculates the time difference, and formats
telnetlib
The use
- Generate CSV files and read and write to files
- In
while True
this dead loop, you need to avoid the CPU going to 100%, you need to add time.sleep(0.2)
the last line.
Next one to talk about these points:
Python3 Get current time
from Import == DateTime.Now ()
In this way, we usually need to format the processing to show the user.
Python3 time Formatting
On the basis above, we can, do it
" %y-%m-%d%h:%m:%s.%f " = Startime.strftime (TimeFormat)
Use strftime
method processing to view the Date.strftime (format) portion of the Python3 document.
Python3 Calculating the time difference
Differencetime = endTime- startime
Yes, just subtract it, and it's done.
Use of Telnetlib
Import= telnetlib. Telnet ("192.168.10.21" "+")
CSV file creation
ImportOSImportCsvcsvfilename="{0}.csv". Format (Nowtime.strftime ("%y-%m-%d"))ifOs.path.exists (Csvfilename) is notTrue:with Open (Csvfilename,"W", newline="") as Csvfile:fieldnames=[Startimetitle, Endtimetitle, Differencetimetitle] writer= csv. Dictwriter (CSVFile, fieldnames=fieldnames) Writer.writeheader ()
This is to determine whether the file exists, if it does not exist, create a CSV file, and write the table header.
CSV file append
" a ", newline=" ") as CSVFile: = [Startimetitle, Endtimetitle, Differencetimetitle] = csv. Dictwriter (CSVFile, fieldnames=fieldnames) writer.writerow ({startimetitle:startimeview, EndTimeTitle: Endtimeview, Differencetime
Dead cycle avoids high CPU
Inside the loop, add one last line:
Import Timetime.sleep (0.2)
Let the thread hibernate for a while, thus avoiding a dead loop that consumes too much CPU.
Using scripts
Python3 windows_telnet.py 192.168.10.21 80
You can then use this script to monitor remote port connection issues and generate a log file every day.
Python Learning Exchange Group: 125240963
Original: Https://www.jianshu.com/p/e2e88cbb7572?utm_source=tuicool&utm_medium=referral
Python test remote port connection time