rule_add.py

来源:互联网 发布:mac下面图标怎么清理 编辑:程序博客网 时间:2024/06/06 21:02

#!/bin/env python

#-*- coding:UTF-8 -*-
import os
os.system("netstat -tn 2>/dev/null | grep :1521 |grep 'ESTABLISHED'| awk '{print $5}' | cut -d: -f 1 | sort | uniq -c | sort -nr >/root/scripts/connect_oracle.txt")
f = open("/root/scripts/connect_oracle.txt", "r")
f1= open("/root/scripts/log.txt","a")
while True:  
    line = f.readline()  
    if line:  
        pass    # do something here
        line=line.strip()
        lnum=line.split(' ')
        if int(lnum[0])>30:
                #print lnum[1]
                ip=lnum[1].split('.')
                #print int(ip[2])
                if int(ip[2]) <> 102:
                        #print lnum[1]
                        print "iptables -I INPUT -s "+lnum[1]+" -p tcp --dport 1521 -j DROP"
                        os.system("/sbin/iptables -I INPUT -s "+lnum[1]+" -p tcp --dport 1521 -j DROP")
                        f1.write("/sbin/iptables -D INPUT -s "+lnum[1]+" -p tcp --dport 1521 -j DROP\n")
                else:
                        pass
        else:
                pass
    else:  
        break
f1.close()

f.close()



删除规则 rule_clear.py

#!/bin/env python
#-*- coding:UTF-8 -*-
import os
f = open("log.txt", "r")
while True:
        line=f.readline()
        if line:
                line=line.strip()
                print line
                os.system(line)
        else:
                break

f.close()

0 0
原创粉丝点击