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