Python monitors host survival and sends an alert via email

Source: Internet
Author: User
Tags email account
This article is a simple test of whether the host is alive by using a python script. This script has a disadvantage that it is not applicable to online applications. Due to network delay and packet loss, it may cause false positives, if you are interested, check whether the Python monitor host is alive and send an email to the police. Use Python to write a simple script to test whether the host is alive. This script is not suitable for online use, if a false positive message is reported due to network delay or packet loss, an alert email is sent after the ping fails three times and multi-thread processing is enabled.

#! /Usr/bin/env python # coding: UTF-8 import time import pexpect import smtplib from email. mime. text import MIMEText mail_host = "smtp.163.com" # define smtp server mail_to = "baojingtongzhi@163.com" # mail recipient mail_from = "monitor@163.com" # mail sender mail_pass = "123456" # mail sender mailbox password while True: def Mail (error_ip): date = time. strftime ('% Y-% m-% d % H: % M: % s') msg = MIMEText ("% S Ping % s failed from 255.252. "% (date, error_ip) msg ['subobject'] =" Ping % s failed. "% error_ip # define the mail subject msg ['from'] = mail_from msg ['to'] = mail_to try: s = smtplib. SMTP () # create an SMTP () object s. connect (mail_host, "25") # connect to the smtp host s. starttls () # Start Secure Transmission Mode s. login (mail_from, mail_pass) # email account logon authentication s. sendmail (mail_from, mail_to, msg. as_string () # mail sent by s. quit () # Disconnect the smtp connection except T Exception, e: print str (e) ip_list = ['100. 168.18.10 ', '2017. 168.18.11 ', '2017. 168.18.12 '] for ip in ip_list: ping = pexpect. spawn ('Ping-c 1% s' % ip) check = ping. secondary CT ([pexpect. TIMEOUT, "1 packets transmitted, 1 received, 0% packet loss"], 2) #2 indicates TIMEOUT if check = 0: Mail (ip) print "Ping % s failed, have email. "% ip if check = 1: print" Ping % s successful. "% ip print" Sleep 10s... "time. sleep (10) # Run it directly # python ping. py Ping 192.168.18.10 successful. ping 192.168.18.11 successful. ping 192.168.18.12 successful. sleep 10 s...

The above is all the content in this article, hoping to help you learn whether the Python monitoring host is alive and send an email alert.

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.