Shell脚本实现检查服务器安全状态(用户、登录IP、防火墙检查)
技术  /  管理员 发布于 8年前   140
说明:大家平时对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 [email protected] " %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 [email protected] " %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 [email protected] " %host
os.system(cmd)
else:
print 'host:%s iptable running ok' %host
user_panduan()
ip_panduan()
iptables_panduan()
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
