Shell正則表達式驗證IP地址
更新時間:2015年05月26日 11:42:37 投稿:junjie
這篇文章主要介紹了Shell正則表達式驗證IP地址,本文給出了多個方法,并分別給出實現(xiàn)代碼,需要的朋友可以參考下
本機多個IP
復(fù)制代碼 代碼如下:
ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'
首先,先用這個來著
復(fù)制代碼 代碼如下:
CheckIPAddress()
{
echo $1 > /tmp/tmpserverip
echo $1 |grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" > /dev/null
if [ $? = 1 ]; then
return 1
else
a=$(cut -d. -f1 /tmp/tmpserverip)
b=$(cut -d. -f2 /tmp/tmpserverip)
c=$(cut -d. -f3 /tmp/tmpserverip)
d=$(cut -d. -f4 /tmp/tmpserverip)
for loop in $a $b $c $d
do
if [ $loop -ge 255 ] || [ $loop -le 0 ]; then
return 2
fi
done
fi
return 0
}
最初的時候,參考過下面的這些
復(fù)制代碼 代碼如下:
grep "^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.$"
輸入ip,檢查其是否滿足的ip書寫規(guī)范,即不能大于255,不能有字母,和其他標(biāo)點,參考網(wǎng)上的,自己搞了個如下,做個標(biāo)記!@
復(fù)制代碼 代碼如下:
echo -n 'Enter the Server-ip:'
read BISSip
echo $BISSip > /tmp/tmpserverip
echo $BISSip|grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" > /dev/null
num=$?
if [ $num = 1 ]
then
echo error ip,please enter correct
else
{
a=$(cut -d. -f1 /tmp/tmpserverip)
b=$(cut -d. -f2 /tmp/tmpserverip)
c=$(cut -d. -f3 /tmp/tmpserverip)
d=$(cut -d. -f4 /tmp/tmpserverip)
{
if [ $a -ge 255 ]||[ $a -le 0 ]
then
echo a:error ip
else
echo 1 > /tmp/jack
fi
}
{
if [ $b -ge 255 ]||[ $b -lt 0 ]
then
echo b:error ip
else
echo 1 >>/tmp/jack
fi
}
{ if [ $c -ge 255 ]||[ $c -lt 0 ]
then
echo c:error ip
else
echo 1 >>/tmp/jack
fi
}
{ if [ $d -ge 255 ]||[ $d -le 0 ]
then
echo d:error ip
else
echo 1 >> /tmp/jack
fi
}
相關(guān)文章
shell的條件測試,變量測試,表達式中的0和1,數(shù)值判斷,字符串判斷
本文主要介紹了shell的條件測試,變量測試,表達式中的0和1,數(shù)值判斷,字符串判斷,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
Linux命令行和shell腳本編程寶典 Richard Blum
Linux命令行和shell腳本編程寶典,主要介紹了linux一些命令的使用2012-09-09
使用shell檢查并修復(fù)mysql數(shù)據(jù)庫表的腳本
這篇文章主要介紹了使用shell檢查并修復(fù)mysql數(shù)據(jù)庫表的腳本,需要的朋友可以參考下2014-03-03

