脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - Shell脚本实现检查服务器安全状态(用户、登录IP、防火墙检查)

Shell脚本实现检查服务器安全状态(用户、登录IP、防火墙检查)

2023-02-24 12:18shell教程网 shell

这篇文章主要介绍了Shell脚本实现检查服务器安全状态,本文主要检查3个方面,分别是系统用户检查、登录IP检查、防火墙状态检查,需要的朋友可以参考下

说明:大家平时对Linux服务器安全主要是对系统用户的检查,登陆服务器IP检查,以及防火墙状态检查!

1.需要把正确系统用户名存储在/root/liu_shell/local_user.txt文件中,然后进行比较!
2.对登陆IP判断是不是以192.168.1和192.168.2开头的IP为正常IP!
3.判断iptables状态!

 

复制代码 代码如下:
 
#!/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 [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 " %host
        os.system(cmd)
    else:
        print 'host:%s iptable running ok' %host
user_panduan()
ip_panduan()
iptables_panduan()

 

延伸 · 阅读

精彩推荐
  • shell图片批量压缩大小脚本分享

    图片批量压缩大小脚本分享

    这篇文章主要介绍了图片批量压缩大小的脚本,需要的朋友可以参考下...

    脚本之家9662023-02-08
  • shellShell脚本模拟多线程功能分享

    Shell脚本模拟多线程功能分享

    这篇文章主要介绍了Shell脚本模拟多线程功能分享,本文直接给出实现代码,代码中有详细的注释,需要的朋友可以参考下...

    shell教程网9522023-02-23
  • shellShell脚本注释写法

    Shell脚本注释写法

    这篇文章主要介绍了Shell脚本注释的3种写法,包含单行注释的方法和多行注释的多种方法,需要的朋友可以参考下...

    shell教程网3912023-02-16
  • shell什么是Shell?Shell脚本基础知识详细介绍

    什么是Shell?Shell脚本基础知识详细介绍

    这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell、有多少种Shell、一个Shell脚本代码实例,需...

    shell教程网10352023-02-15
  • shellShell脚本echo指令使用小技巧

    Shell脚本echo指令使用小技巧

    这篇文章主要介绍了Shell脚本echo指令使用小技巧,包括使用echo指令输出换行、输出不换行、输出变量等技巧,需要的朋友可以参考下...

    shell教程网4322023-02-16
  • shell后台实时分流文件的shell脚本

    后台实时分流文件的shell脚本

    后台实时分流文件的shell脚本,有需要的朋友可以参考下...

    脚本之家4902022-12-09
  • shellshell数组常用实例分享

    shell数组常用实例分享

    本文为大家举一些shell数组的小例子,供大家学习参考...

    shell教程网4452022-12-16
  • shell利用kernel提供的接口打印进程号(pid)

    利用kernel提供的接口打印进程号(pid)

    我们知道linux是模块化的内核。实现模块、利用kernel提供的接口,首先了解写模块的基本框架。下面的c文件就是最基本的框架,当然还有怎样添加一些符号...

    脚本之家8362022-12-22