Shell script Implementation Check Server security status (user, login IP, firewall check) _linux Shell

Source: Internet
Author: User
Tags socket iptables

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:

#coding =utf-8
Import Sys,os,re,socket
Host=str (Socket.gethostname (). Strip ())
Def User_panduan ():
File01=file ('/etc/passwd ')
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 ')
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
Cmd= "Echo ' host:%s user error ' |mail-s user_error"%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 ')
For line in F_IP:
Line=line.strip ()
Local_ip.append (line)
Kk=re.match (' 192.168.1|192.168.2 ', AA)
If KK:
print ' host:%s ip ok '%host
Cmd= "Echo ' host:%s IP error ' |mail-s ip_error"%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 '"
Os.system (CMD)
print ' host:%s iptable running OK '%host
User_panduan ()
Ip_panduan ()
Iptables_panduan ()

Related Article

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: 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.