Get the App
SLTechnology News&Howtos  ›  Servers  › 

Use python and batch bat script ping to test host connectivity

Shulou Source: shulou.com Published: 2022-06-02 08:17:17 09月17日 Update

Today, due to work requirements, it is necessary to check the network availability connected with the hands-on on the offer machine (windows system). Using the function of zabbix itself can not solve this problem. So at the beginning, consider using the ping command of the python script tuning system to detect

Then use zabbix to monitor and complete the task.

After looking it up on the Internet, I found that the test can be completed by using subprocess and shlex, so I referred to the whole small script.

#! / usr/bin/env python#-*-coding: utf-8-*-# author:qingmiaoimport subprocessimport shlexcmd = "ping-n 1 127.0.0.1" args = shlex.split (cmd) try: subprocess.check_call (args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print "1" except subprocess.CalledProcessError: print "0"

After execution, as shown in the following figure, 1 is returned after successful detection, otherwise 0 is returned.

Later, it was found that this method was not documented on the ground and was not easy to track. Tried a lot of methods, and finally found that the use of windows's own bat script can be easily achieved, amazed!

Redirect the execution results to a file named after a timestamp, which is time-divided to facilitate tracking of the results.

@ echo offset THISDATE=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%set IP=127.0.0.1echo% TIME:~0,8% > > D:\ test\ ping_result_%THISDATE%.txt (ping% IP%-n 1 | find "TTL" > > D:\ test\ ping_result_%THISDATE%.txt) & & echo 1 | | echo 0

The execution result is as follows, and you can also use zabbix to call this script

BINGO

Tags: Scripts tests results files time systems surprises successes surprises tasks functions availability commands methods machines networks problems requirements utf-8 Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple macOS Docker NVidia