ShellRegex驗證IP地址

來源:互聯網
上載者:User

ShellRegex驗證IP地址

   這篇文章主要介紹了ShellRegex驗證IP地址,本文給出了多個方法,並分別給出實現代碼,需要的朋友可以參考下

  本機多個IP

  代碼如下:

  ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

  首先,先用這個來著

   代碼如下:

  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

  }

  最初的時候,參考過下面的這些

   代碼如下:

  grep "^([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|25[0-5]).$"

  輸入ip,檢查其是否滿足的ip書寫規範,即不能大於255,不能有字母,和其他標點,參考網上的,自己搞了個如下,做個標記!@

   代碼如下:

  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

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.