Shell正則表達(dá)式驗(yàn)證IP地址
本機(jī)多個(gè)IP
ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'
首先,先用這個(gè)來(lái)著
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
}
最初的時(shí)候,參考過(guò)下面的這些
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)點(diǎn),參考網(wǎng)上的,自己搞了個(gè)如下,做個(gè)標(biāo)記!@
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腳本中實(shí)現(xiàn)把輸入的密碼轉(zhuǎn)換為*(星號(hào))的方法
這篇文章主要介紹了Shell腳本中實(shí)現(xiàn)把輸入的密碼轉(zhuǎn)換為*(星號(hào))的方法,從而把密碼字符串隱藏起來(lái),比較實(shí)用的一個(gè)功能,在做交互設(shè)計(jì)時(shí)非常有用哦,需要的朋友可以參考下2014-06-06一個(gè)用了統(tǒng)計(jì)CPU 內(nèi)存 硬盤 使用率的shell腳本
一個(gè)統(tǒng)計(jì) CPU 內(nèi)存 硬盤 使用率的shell腳本,供大家學(xué)習(xí)參考2013-02-02遠(yuǎn)程SSH連接服務(wù)與基本排錯(cuò)經(jīng)驗(yàn)總結(jié)
下面小編就為大家?guī)?lái)一篇遠(yuǎn)程SSH連接服務(wù)與基本排錯(cuò)經(jīng)驗(yàn)總結(jié)。小編覺得聽不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Shell腳本實(shí)現(xiàn)根據(jù)端口號(hào)kill相應(yīng)進(jìn)程功能
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)根據(jù)端口號(hào)kill相應(yīng)進(jìn)程功能,本文相對(duì)簡(jiǎn)單,只有一句話,需要的朋友可以參考下2014-12-12Git 創(chuàng)建分支提交遠(yuǎn)程分支詳解
這篇文章主要介紹了Git 創(chuàng)建分支提交遠(yuǎn)程分支詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04