Shell check string in Linux is not a valid IP

Source: Internet
Author: User

Example

The code is as follows Copy Code

zj@zj:~/script/cushell/08.11.04$ Cat checkip.sh
View Plainprint?
#!/bin/bash


Printerr () {
echo "Incorrect IP format."
Readip
Chkip
}

Readip () {
Read-p "Your IP:" IP
}

Chkip () {
echo "$IP" | Grep-eq ' [^0-9.]| ^\.| \.$|^0*\.| \.\. ' && printerr
[$ (echo-e "${ip//./\n}" | wc-l)-ne 4] && Printerr
For i in ${ip//./}; Todo
echo $i | Grep-eq "^0+[^0]" && printerr
[$ ((10# $i/8))-GT] && Printerr

Done
}

If ["$"]; Then
Ip=$1
Else
Readip
Fi
Chkip
echo "$IP is good!"

Explained below:

Grep-eq ' [^0-9.]| ^\.| \.$|^0*\.| \.\.'
[^0-9.] Is there a character other than 0-9 and.
^\.| \.$ at the beginning of or at the end of.
^0*\.| \.\. With 0 opening or two consecutive.
All three of these things are wrong oh, so just Printerr

Echo-e "${ip//./\n}" | Wc-l, to be honest, I'm also first in the first place. Replace with line, 3 points is 4 line oh. It's not 3 points, it's printerr.

$ ((10# $i/8))-GT 31 This is the judgment is not <=255, of course, you can also modify the $I-GT 255

OK explain complete ^_^

The code is as follows Copy Code

zj@zj:~/script/cushell/08.11.04$./checkip.sh 1.2.3.4
1.2.3.4 is
zj@zj:~/script/cushell/08.11.04$./checkip.sh 01.2.3.4
Incorrect IP format.
Your IP:A.B.A.D
Incorrect IP format.
Your ip:266.1.1.1
Incorrect IP format.
Your ip:244.255.255.255
244.255.255.255 is

Example 2, using the shell to verify the legality of IP address

How to use:

The code is as follows Copy Code


[Root@yang python]# bash check_ip.sh IP address

Execution Result: The return value of 0 checksum is valid, not 0 illegal.

Shell code:

The code is as follows Copy Code

[Root@yang python]# VI check_ip.sh
#!/usr/bin/sh
CHECKIPADDR ()
{
echo $1|grep "^[0-9]\{1,3\}\.\ ([0-9]\{1,3\}\.\) \{2\}[0-9]\{1,3\}$" >/dev/null;
#IP地址必须为全数字
If [$?-ne 0]
Then
Return 1
Fi
Ipaddr=$1
A= ' echo $ipaddr |awk-f. ' {print '} ' #以. ' Separate, remove the value of each column
B= ' echo $ipaddr |awk-f. ' {print $} '
C= ' echo $ipaddr |awk-f. ' {print $} '
D= ' echo $ipaddr |awk-f. ' {print $} '
For num in $a $b $c $d
Todo
If [$num-gt 255] | | [$num-lt 0] #每个数值必须在0-255
Then
Return 1
Fi
Done
return 0
}
If [$#-ne 1];then #判断传参数量
echo "Usage: $ ipaddress."
Exit
Else
CHECKIPADDR $
Fi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.