Description: Everyone at ordinary times to Linux server security is mainly to the system user inspection, landing server IP check, as well as firewall status check!
1. You need to store the correct system username in the/root/liu_shell/local_user.txt file, and then compare!
2. To the landing IP to determine whether to 192.168.1 and 192.168.2 IP for the normal ip!
3. Judge the state of Iptables!
Copy Code code as follows:
#!/usr/bin/python
#coding =utf-8
Import Sys,os,re,socket
Host=str (Socket.gethostname (). Strip ())
Fuhao=os.linesep
Def User_panduan ():
File01=file ('/etc/passwd ')
Mmm=[]
For XX in file01:
Mmm.append (Re.split (': ', XX) [0])
File01.close ()
File02=file ('/root/liu_shell/new_user.txt ', ' W ')
For yy in MMM:
File02.write ('%s%s '% (Yy,fuhao))
File02.close ()
F_local=file ('/root/liu_shell/local_user.txt ')
F_new=file ('/root/liu_shell/new_user.txt ')
Local_user=[]
New_user=[]
For line1 in f_local:
Line1=line1.strip ()
Local_user.append (line1)
For line2 in F_new:
Line2=line2.strip ()
New_user.append (line2)
F_local.close ()
F_new.close ()
If Local_user==new_user:
print ' host:%s user ok '%host
Else
Cmd= "Echo ' host:%s user error ' |mail-s user_error 331095659@qq.com"%host
Os.system (CMD)
Def Ip_panduan ():
Os.system ("Last|awk ' {print $} ' |grep-v [a-z]|grep-v ^$|sort |uniq >/root/liu_shell/local_ip.txt")
F_ip=file ('/root/liu_shell/local_ip.txt ')
Local_ip=[]
For line in F_IP:
Line=line.strip ()
Local_ip.append (line)
For AA in LOCAL_IP:
Kk=re.match (' 192.168.1|192.168.2 ', AA)
If KK:
print ' host:%s ip ok '%host
Else
Cmd= "Echo ' host:%s IP error ' |mail-s ip_error 331095659@qq.com"%host
Os.system (CMD)
Def Iptables_panduan ():
Iptables_status=int (Os.popen ("/sbin/iptables-nl|grep-v ^$|wc-l"). ReadLine (). Strip ())
If iptables_status==6:
Cmd= "Echo ' host:%s iptables not running! ' |mail-s iptables ' 331095659@qq.com"
Os.system (CMD)
Else
print ' host:%s iptable running OK '%host
User_panduan ()
Ip_panduan ()
Iptables_panduan ()