目录
1、屏蔽每分钟访问超过200的IP
方法1:以Nginx日志作为测试
#!/bin/bash
DATE=$(date +%d/%b/%Y:%H:%M)
ABNORMAL_IP=$(tail -n5000 access.log |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>100)print i}')
#先tail防止文件过大,读取慢,数字可调整每分钟最大的访问量。awk不能直接过滤日志,因为包含特殊字符。
for IP in $ABNORMAL_IP; do
if [ $(iptables -vnL |grep -c "$IP") -eq 0 ]; then
iptables -I INPUT -s $IP -j DROP
fi
done
方法2:通过建立连接数
ABNORMAL_IP=$(netstat -an |awk '$4~/:80$/ && $6~/ESTABLISHED/{gsub(/:[0-9]+/,"",$5);{a[$5]++}}END{for(i in a)if(a[i]>100)print i}')
#gsub是将第五列(客户端IP)的冒号和端口去掉
for IP in $ABNORMAL_IP; do
if [ $(iptables -vnL |grep -c "$IP") -eq 0 ]; then
iptables -I INPUT -s $IP -j DROP
fi
done
#!/bin/bash
# Description: Only CentOS6
traffic_unit_conv() {
local traffic=$1
if [ $traffic -gt 1024000 ]; then
printf "%.1f%s" "$(($traffic/1024/1024))" "MB/s"
elif [ $traffic -lt 1024000 ]; then
printf "%.1f%s" "$(($traffic/1024))" "KB/s"
fi
}
NIC=$1
echo -e " In ------ Out"
while true; do
OLD_IN=$(awk -F'[: ]+' '$0~"'$NIC'"{print $3}' /proc/net/dev)
OLD_OUT=$(awk -F'[: ]+' '$0~"'$NIC'"{print $11}' /proc/net/dev)
sleep 1
NEW_IN=$(awk -F'[: ]+' '$0~"'$NIC'"{print $3}' /proc/net/dev)
NEW_OUT=$(awk -F'[: ]+' '$0~"'$NIC'"{print $11}' /proc/net/dev)
IN=$(($NEW_IN-$OLD_IN))
OUT=$(($NEW_OUT-$OLD_OUT))
echo "$(traffic_unit_conv $IN) $(traffic_unit_conv $OUT)"
sleep 1
done
# 也可以通过ficonfig命令获取收发流量
while true; do
OLD_IN=$(ifconfig $NIC |awk -F'[: ]+' '/bytes/{print $4}')
OLD_OUT=$(ifconfig $NIC |awk -F'[: ]+' '/bytes/{print $9}')
sleep 1
NEW_IN=$(ifconfig $NIC |awk -F'[: ]+' '/bytes/{print $4}')
NEW_OUT=$(ifconfig $NIC |awk -F'[: ]+' '/bytes/{print $9}')
IN=$(($NEW_IN-$OLD_IN))
OUT=$(($NEW_OUT-$OLD_OUT))
echo "$(traffic_unit_conv $IN) $(traffic_unit_conv $OUT)"
sleep 1
done
1)检查URL可用性
方法1:
check_url() {
HTTP_CODE=(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}"
if
[ $HTTP_CODE -ne
200 ]; then
echo
"Warning: $1 Access failure!"
fi
}
方法2:
check_url() {
if
! wget -T 10 --tries=1 --spider $1 >/dev/null
2>&1; then
#-T超时时间,--tries尝试1次,--spider爬虫模式
echo
"Warning: $1 Access failure!"
fi
}
使用方法:check_url www.baidu.com
2)判断三次URL可用性
思路与上面检查主机存活状态一样。
方法1:利用循环技巧,如果成功就跳出当前循环,否则执行到最后一行
#!/bin/bash
check_url() {
HTTP_CODE=(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}"
if
[ $HTTP_CODE -eq
200 ]; then
continue
fi
}
URL_LIST="www.baidu.com www.agasgf.com"
for
URL in
$URL_LIST; do
check_url $URL
check_url $URL
check_url $URL
echo
"Warning: $URL Access failure!"
done
方法2:错误次数保存到变量
#!/bin/bash
URL_LIST="www.baidu.com www.agasgf.com"
for
URL in
$URL_LIST; do
FAIL_COUNT=0
for
((i=1;i<=3;i++)); do
HTTP_CODE=(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}"
if
[ $HTTP_CODE -ne
200 ]; then
let
FAIL_COUNT++
else
break
fi
done
if
[ $FAIL_COUNT -eq
3 ]; then
echo
"Warning: $URL Access failure!"
fi
done
方法3:错误次数保存到数组
#!/bin/bash
URL_LIST="www.baidu.com www.agasgf.com"
for
URL in
$URL_LIST; do
NUM=1
while
[ $NUM -le
3 ]; do
HTTP_CODE=(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}"
if
[ $HTTP_CODE -ne
200 ]; then
FAIL_COUNT[NUM]=IP #创建数组,以NUM下标,IP元素
let
NUM++
else
break
fi
done
if
[ ${#FAIL_COUNT[*]} -eq 3 ]; then
echo
"Warning: $URL Access failure!"
unset
FAIL_COUNT[*] #清空数组
fi
done
#!/bin/bash
URL="www.baidu.com www.sina.comwww.jd.com"
for url in $URL; do
HTTP_CODE=$(curl -o /dev/null -s -w %{http_code} http://$url)
if [ $HTTP_CODE -eq 200 -o $HTTP_CODE -eq 301 ]; then
echo "$url OK."
else
echo "$url NO!"
fi
done
#!/bin/bash
HOST=$1
PORT="22 25 80 8080"
for PORT in $PORT; do
if echo &>/dev/null > /dev/tcp/$HOST/$PORT; then
echo "$PORT open"
else
echo "$PORT close"
fi
done
比如:本机的ip地址是: 192.168.100.5/255.255.255.0, 那么他的网络地址是: 192.168.100.1/255.255.255.0 方法一:
#!/bin/bash
IP=ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g'|awk '{print $1}'
NETMASK=ifconfig eth0 |grep "inet addr"|sed 's/^.*Mask://g'
echo "$IP/&NETMASK"
exit
方法二:
#!/bin/bash
#This script print ip and network
file="/etc/sysconfig/network-scripts/ifcfg-eth0"
if [ -f $file ] ;then
IP=`grep "IPADDR" $file|awk -F"=" '{ print $2 }'`
MASK=`grep "NETMASK" $file|awk -F"=" '{ print $2 }'`
echo "$IP/$MASK"
exit 1
fi
IP地址也可这样获取:IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " " 子网掩码:NETMASK= `ifconfig eth0 | grep "inet addr"|cut -f 4 -d ":"
方法1:
function check_ip(){
IP=$1
VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$">/dev/null; then
if [ $VALID_CHECK == "yes" ]; then
echo "$IP available."
else
echo "$IP not available!"
fi
else
echo "Format error!"
fi
}
check_ip 192.168.1.1
check_ip 256.1.1.1
方法2:
function check_ip(){
IP=$1
if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
FIELD1=$(echo $IP|cut -d. -f1)
FIELD2=$(echo $IP|cut -d. -f2)
FIELD3=$(echo $IP|cut -d. -f3)
FIELD4=$(echo $IP|cut -d. -f4)
if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
echo "$IP available."
else
echo "$IP not available!"
fi
else
echo "Format error!"
fi
}
check_ip 192.168.1.1
check_ip 256.1.1.1
增加版:加个死循环,如果IP可用就退出,不可用提示继续输入,并使用awk判断
function check_ip(){
local IP=$1
VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
if [ $VALID_CHECK == "yes" ]; then
return 0
else
echo "$IP not available!"
return 1
fi
else
echo "Format error! Please input again."
return 1
fi
}
while true; do
read -p "Please enter IP: " IP
check_ip $IP
[ $? -eq 0 ] && break || continue
done
方法3
function check_ip(){
IP=$1
if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
FIELD1=$(echo $IP|awk -F "." '{print $1}')
FIELD2=$(echo $IP|awk -F "." '{print $2}')
FIELD3=$(echo $IP|awk -F "." '{print $3}')
FIELD4=$(echo $IP|awk -F "." '{print $4}')
if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
echo "$IP available."
else
echo "$IP not available!"
fi
else
echo "Format error!"
fi
}
check_ip 192.168.0.29
check_ip 288.2.3.4