Shel 檢測合法的IP位址

2021-06-09 22:40:30 字數 973 閱讀 7154

(([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5]).)([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])'

1.利用電腦ip測試下:

ifconfig | grep --color -o  '\(\([0-9]\|[1-9][0-9]\|1[0-9]\\|2[0-4][0-9]\|25[0-5]\)\.\)\\([0-9]\|[1-9][0-9]\|1[0-9]\\|2[0-4][0-9]\|25[0-5]\)'

2. 利用字串測試下:

mm="79171 dns resolve: img02.taobaocdn.com => 61.183.52.250 (from cache)"

ip=`echo $mm | grep -o '\(\([0-9]\|[1-9][0-9]\|1[0-9]\\|2[0-4][0-9]\|25[0-5]\)\.\)\\([0-9]\|[1-9][0-9]\|1[0-9]\\|2[0-4][0-9]\|25[0-5]\)' | wc -l`

if [ $ip -eq 1 ];then

echo "valid ip"

else

echo "invalid ip"

fi

3. 換一種方式,不用grep來匹配,而用~來匹配測試下:

iip="192.168.23.1"

if [[ $iip =~ ^(([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])\.)([0-9]|[1-9][0-9]|1[0-9]|2[0-4][0-9]|25[0-5])$ ]]; then

echo "ip"

else

echo "invalid ip"

fi

判斷IP位址是否合法

1 字串切割判斷 def legalip ip ip ip.split m len ip if m 4or not ip 0 isdigit orint ip 0 0or int ip 0 255 print illegal else for i in range 1 m if ip i isdig...

C 輸出IP位址段內的合法位址

近半年的intel實習生活快要結束了.馬上要找工作了,這段時間打算把以前的知識複習複習,順便在這裡記錄一下.這是當時去intel面試的時候,面試官問的一道題.當時因為時間關係,只讓我提供乙個思路,並沒有寫出具體實現過程.下面把實現過程寫上.主要是把ip位址轉換成整數,這樣比擷取ip位址每一段內的值出...

判斷輸入的IP位址是否合法

判斷輸入的ip位址是否合法,ip位址的值在0 255之間,先把輸入的ip的位址轉換為乙個字串。define len sizeof xx sizeof xx 0 const char xx const char m int checkipisvalid const char ip char c 4 i...