Linux Server login mail alert python program

Source: Internet
Author: User

Linux Server user Login email alert python applet, once someone logs in to the server, send an email to the system administrator. As SA some servers only own can log in, you understand! Can sometimes be developed pits, are transported to the pit. In order to fully control the server, configure a user login system mail notification, is very necessary drip!

A script that compares slag residue, but can be easily extended on a sub-basis. directly on the code.

#!/usr/bin/python
#coding: UTF8
Import Smtplib
Import string
Import Psutil
Import datetime


HOST = "Smtp.qq.com"
SUBJECT = "Server logon Information"
to = "[Email protected]"
from = "[Email protected]"
StartTime = Datetime.datetime.now ()
Text = str (starttime) + "Someone logged in to the server, please view"
BODY = String.Join ((
' From:%s '% from,
' To:%s '% to,
"Subject:%s"% Subject,
"",
Text
), "\ r \ n")

Users_count = Len (Psutil.users ())
Users_list = ",". Join ([U.name for U in Psutil.users ()])
def sendemail (users_count,users_list):
Server = Smtplib. SMTP ()
Server.connect (HOST, "25")
Server.starttls ()
Server.login ("[Email protected]", "password")
If Users_count > 0:
Server.sendmail (from, [to], BODY)
Server.quit ()
Else
Print "Program is bak"
if __name__ = = ' __main__ ':
SendEmail (Users_count,users_list)

The main use of the Linux system comes with the Python send mail module smtplib, and Psutil module to check the number of system logins. As long as it is greater than 0, it is certain that someone is logged in. Slag script, I will not crap.










Linux Server login mail alert python program

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.