Using Python and the batch bat script ping to detect host connectivity

Source: Internet
Author: User
Tags python script

today, because of the need for work, it is not possible to use Zabbix's own functionality to detect network availability on an offer machine (Windows system) that is connected to the Web. So start by thinking about using the Python script to tune the system's ping command to detect

You can then use Zabbix to monitor and complete the task.

Online check, found that using subprocess and Shlex can complete the detection, and then refer to the entire 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, the detection returns 1 successfully, otherwise 0

Later found that this method did not land into documents, not easy to track. Tried a lot of ways, and finally found that the use of Windows comes with the bat script can be easily implemented, pleasantly surprised!

Redirect execution results to a timestamp-named file that is split in time to facilitate tracking results.



@echo offset Thisdate=%date:~0,4%%date:~5,2%%date:~8,2%set Ip=10.10.83.131echo%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 results are as follows, and you can also use Zabbix to invoke the script

BINGO

Using Python and the batch bat script ping to detect host connectivity

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.