Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write Shell script to check the security status of the server

Shulou Source: shulou.com Published: 2022-06-03 09:19:11 11月15日 Update

This article introduces the relevant knowledge of "how to write Shell scripts to check the security status of the server". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Note: usually the security of the Linux server is mainly the check of the system users, the login server IP check, and the firewall status check!

1. You need to store the correct system user name in the / root/liu_shell/local_user.txt file and compare it!

two。 To determine whether the login IP starts with 192.168.1 and 192.168.2 IP is a normal IP!

3. Judge the status of iptables!

The code is 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 $3}'| grep-v [amurz] | 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)

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 runningbacks' | mail-s iptables 331095659@qq.com"% host

Os.system (cmd)

Else:

Print 'host:%s iptable running ok'% host

User_panduan ()

Ip_panduan ()

Iptables_panduan ()

This is the end of "how to write Shell scripts to check the security status of the server". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Check server status service security script content more user knowledge system login utility learning next code dilemma actual beginning situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Apple Shulou Technology OPPO Reno Shulou Tech Info