| 知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
進程間通信就是在不同進程之間傳播或交換信息。
腳本具有黑白名單功能,一個進程專門負責採集數據,另一個進程專門負責處理由第一個進程發送過來的數據。
#!/bin/bash
########################################
# Homepage: http://netkiller.github.io
# Author: neo <netkiller@msn.com>
########################################
BLACKLIST=/tmp/BLACKLIST.lst
PIPE=/tmp/pipe
pidfile=/tmp/firewall.pid
KEYWORD=XXDD0S
ACCESSLOG=/www/logs/www.example.com/access.$(date +'%Y-%m-%d').log
########################################
if [ -z $1 ]; then
echo "$0 clear|fw|collect|process|close"
fi
if [ "$1" == "clear" ]; then
rm -rf $BLACKLIST
rm -rf $PIPE
echo "Clear OK!!!"
fi
if [ "$1" == "close" ]; then
kill `cat $pidfile`
echo > $pidfile
fi
if [ ! -f $BLACKLIST ]; then
touch $BLACKLIST
fi
if [ ! -e $PIPE ]; then
mkfifo $PIPE
fi
if [ "$1" == 'fw' ]; then
iptables -A OUTPUT -p tcp --dport 2049 -j REJECT
iptables -A OUTPUT -p tcp -m multiport --dports 22,21 -j REJECT
fi
if [ "$1" == "collect" ]; then
killall tail
for (( ; ; ))
do
tail -f $ACCESSLOG | grep $KEYWORD | cut -d ' ' -f1 > $PIPE
done &
echo $! > $pidfile
fi
if [ "$1" == "process" ]; then
for (( ; ; ))
do
while read line
do
grep $line ${BLACKLIST}
if [ $? -eq 1 ] ; then
echo $line >> ${BLACKLIST}
iptables -I INPUT -p tcp --dport 80 -s $line -j DROP
fi
done < $PIPE
done &
echo $! >> $pidfile
fi
首先啟動第一個進程,準備接收數據
# ipfw process
然後啟動第二個進程,發送採集數據
# ipfw collect
這個程序使用管道作為進程見通信手段,所以只能在一個系統下運行,如果改為Socket通信就可以實現跨伺服器數據處理