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)