This article describes the Python implementation of user login email notification method. Share to everyone for your reference. Specific as follows:
This is written in Linux scheduled tasks scheduled execution, when a new user logged in when the user name to the designated mailbox notification administrator.
#!/usr/bin/env python#encoding=utf-8from smtplib Import smtpimport subprocesssmtp = "smtp.qq.com" user = ' 1234567 ' password = ' xxxx ' RUN_COMD = subprocess. Popen (' W¦grep pts ', shell=true,stdout=subprocess. PIPE) data = Run_comd.stdout.read () mailb = ["The server has a new user login", Data]mailh = ["from:1234567@qq.com", "to:xxxx@gmail.com", " Subject: User Login Monitoring "]mailmsg =" \r\n\r\n ". Join ([" \ r \ n ". Join (Mailh)," \ r \ n "). Join (MAILB)]) def send_mail (): send = SMTP ( SMTP) send.login (user,password) result = Send.sendmail ("1234567@qq.com", ("xxxx@gmail.com",), MailMsg) send.quit () if data = = ": passelse: send_mail ()
Hopefully this article will help you with Python programming.