Shell腳本實(shí)現(xiàn)檢查服務(wù)器安全狀態(tài)(用戶、登錄IP、防火墻檢查)
說(shuō)明:大家平時(shí)對(duì)Linux服務(wù)器安全主要是對(duì)系統(tǒng)用戶的檢查,登陸服務(wù)器IP檢查,以及防火墻狀態(tài)檢查!
1.需要把正確系統(tǒng)用戶名存儲(chǔ)在/root/liu_shell/local_user.txt文件中,然后進(jìn)行比較!
2.對(duì)登陸IP判斷是不是以192.168.1和192.168.2開(kāi)頭的IP為正常IP!
3.判斷iptables狀態(tài)!
#!/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()
相關(guān)文章
shell之創(chuàng)建文件及內(nèi)容的方法示例
這篇文章主要介紹了shell之創(chuàng)建文件及內(nèi)容的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Linux BASH多進(jìn)程并行處理的方法實(shí)現(xiàn)
Linux下BASH多進(jìn)程并行處理的實(shí)現(xiàn)代碼,需要的朋友可以參考下2013-01-01Shell腳本實(shí)戰(zhàn)之DNS主從同步腳本實(shí)例
這篇文章主要介紹了Shell腳本實(shí)戰(zhàn)之DNS主從同步腳本實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10