Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use python and batch bat script ping to test host connectivity

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report