Python Exercises (iv)

Source: Internet
Author: User

Write a script to determine whether the 80 port of the machine is open, if you do not open anything, if the discovery of the port does not exist, then restart the httpd service, the concurrent mail notify you yourself. Once the script is written, it can be executed every minute, and a dead loop script can be written, and 30s is detected once.


#!/usr/bin/env python

#!coding=utf-8

Import OS

Import time

Import Sys

Import Smtplib

From Email.mime.text import Mimetext

From email. Mimemultipart Import Mimemultipart


def sendsimplemail (warning):

msg = Mimetext (warning)

msg[' Subject ' = ' python first mail '

Msg[' from '] = ' [email protected] '

Try

SMTP = Smtplib. SMTP ()

Smtp.connect (R ' smtp.126.com ')

Smtp.login (' [email protected] ', ' 13716370012 ')

Smtp.sendmail (' [email protected] ', [' [email protected] '], msg.as_string ())

Smtp.close ()

Except Exception, E:

Print E


While True:

Http_status = Os.popen (' netstat-tulnp | grep httpd ', ' R '). ReadLines ()

Try

if http_status = = []:

Os.system (' service httpd start ')

New_http_status = Os.popen (' netstat-tulnp | grep httpd ', ' R '). ReadLines ()

STR1 = ". Join (New_http_status)

is_80 = Str1.split () [3].split (': ') [-1]

If is_80! = ' 80 ':

print ' httpd failed to start '

Else

print ' httpd start successful '

Sendsimplemail (warning = "This is a warning!!!")

Else

print ' httpd normal '

Time.sleep (5)

Except Keyboardinterrupt:

Sys.exit (' \ n ')



Python Exercises (iv)

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.