ip的合法性判斷

#!/bin/bash
#2017830日
#by lee
#auto test the ip

IP_ADDR=$1
IP_DIR="ip.txt"
test_ipA(){
    if [[ $1 -gt 254 ]] || [[ $1 -le 0 ]];then
        echo "the ip is illegal"
        exit 0
    fi
}
test_ipB(){
        if [[ $1 -gt 254 ]] || [[ $1 -lt 0 ]];then
                echo "the ip is illegal"
                exit 0
        fi
}
echo $IP_ADDR > $IP_DIR
grep --color -E "([0-9]{1,3}.){3}[0-9]{1,3}" $IP_DIR > /dev/null
if [[ $? -ne 0 ]];then
    echo "the ip is illegal"
    exit 0
else
    a=`awk -F "." '{print $1}' ${IP_DIR}`
    #傳遞參數給函數
    test_ipA "$a"
    b=`awk -F "." '{print $2}' ${IP_DIR}`
    test_ipB $b
    c=`awk -F "." '{print $3}' ${IP_DIR}`
    test_ipB $c
    d=`awk -F "." '{print $4}' ${IP_DIR}`
    test_ipA $d
    echo "the ip is legal"
fi

發佈了44 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章