2012-02-14 55 views
0

我想驗證短劃線腳本內的IP地址。我發現很多方法可以實現使用bash相同,如linuxjournal驗證一個IP在破折號(而不是bash)

基本上是什麼做用這樣的對比:

if [[ $ip =~ '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ]]; then 
    do something 
fi 

有沒有什麼辦法讓與前圍一樣嗎?

UPDATE:這是最後的腳本,做我需要的東西:

#In case RANGE is a network range (cidr notation) it's splitted to every ip from 
# that range, otherwise we just keep the ip 
if echo $RANGE | grep -E -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}$'; then 
    IPS=`prips $RANGE -e ...0,255` 
    if [ "$?" != "0" ] ; then 
     echo "ERROR: Not a valid network range!" 
     exit 1 
    fi 
elif echo $RANGE | grep -E -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'; then 
    IPS="$RANGE" 
else 
    echo "$RANGE no is not a valid IP address or network range" 
    exit 1 
fi 
+0

IP地址只是一個32位數字。你要求的是四角符號,但請記住十進制和十六進制符號同樣有效。 '0x1020304'和'16909060'與'1.2.3.4'相同。 – ghoti 2012-02-14 12:26:42

+0

我只需要驗證人爲錯誤,除了我和我的同事之外沒有人會使用這個腳本,所以我可以選擇一個符號並堅持。 – user322049 2012-02-14 13:18:19

+0

關於您的最終正則表達式,您可能會將其表示爲稍短一點:'^([0-9] {1,3} \。){3} [0-9] {1,3} \/[0-9] {1,2} $' – ghoti 2012-02-15 18:39:59

回答

0

假設你很高興與驗證串:

 
$ s='[0-9]\{1,3\}' 
$ echo $ip | grep > /dev/null "^$s\.$s\.$s\.$s$" && 
    echo $ip is valid 

請注意,此接受像無效的IP地址876.3.4.5

要驗證一個IP,使用正則表達式確實不方便。的相對容易的事是:

 
IFS=. read a b c d << EOF 
$ip 
EOF 

if (for i in a b c d; do 
     eval test \$$i -gt 0 && eval test \$$i -le 255 || exit 1 
    done 2> /dev/null) 
then 
    echo $ip is valid 
fi 
+0

好,唯一的問題是我需要-E標誌來獲得正則表達式(或使用egrep) 非常感謝 – user322049 2012-02-14 11:30:49

+0

擴展正則表達式應該只是如果您使用s ='[0-9] {1,3}',則需要。如果你轉義括號,基本的正則表達式應該工作。 (啊,你在我的編輯之前發表了你的評論...) – 2012-02-14 12:26:34

+0

此外,重定向的東西有點奇怪。只需使用'egrep -q「regex」'代替。 – ghoti 2012-02-14 12:28:26

1

您可以構建一個case聲明,儘管它會比正則表達式更詳細。另一方面,您避免產生任何外部進程,並且可能更易於閱讀和維護以啓動。

if case $ip in 
    # Invalid if it contains any character other than number or dot 
    # Invalid if it contains more than three dots 
    # Invalid if it contains two adjacent dots 
    # Invalid if it begins with a non-number 
    # Invalid if it ends with a non-number 
    *[!.0-9]* | *.*.*.*.* | *..* | [!0-9]* | *[!0-9]) false ;; 
    # Invalid if it contains a number bigger than 255: 
    # 256-259 or 260-299 or 300-999 or four adjacent digits 
    *25[6-9]* | *2[6-9][0-9]* | *[3-9][0-9][0-9]* | *[0-9][0-9][0-9][0-9]*) false;; 
    # Verify that it contains three dots 
    *.*.*.*) true ;; 
    # Failing that, invalid after all (too few dots) 
    *) false ;; 
esac; then 
    echo "$ip" is valid 
fi 

通知時髦使用case聲明的if語句(返回true或false)爲條件的。

這比正則表達式稍微更嚴格的,因爲它要求每個八位位組,以小於256。

+0

我不喜歡必須調試這樣的功能...:/ – zrajm 2014-07-22 23:56:17

0

這裏是一個小dash殼功能其不使用任何外部命令並且檢查是否一個IPv4地址有效。如果地址格式正確,則返回true,否則返回false。

我試着在我的評論中解釋魔術。

valid_ip() { 
    local IP="$1" IFS="." PART   # make vars local, get arg, set $IFS 
    set -- $IP       # split on $IFS, set $1, $2 etc. 
    [ "$#" != 4 ] && return 1   # BAD: if not 4 parts 
    for PART; do       # loop over $1, $2 etc. 
     case "$PART" in 
      *[!0-9]*) return 1   # BAD: if $PART contains non-digit 
     esac 
     [ "$PART" -gt 255 ] && return 1 # BAD: if $PART > 255 
    done 
    return 0        # GOOD: nothing bad found 
} 

使用此功能,您可以測試您的IP地址,例如,如果IP地址無效,請中止腳本:

if valid_ip "$IP"; then 
    echo "ERROR: IP address '$IP' is invalid" >&2 
    exit 4 
fi