The user enters the argument to the script IP address, tell me how to check the correctness of the input of the IP address with the help of regulars?
4 answers
echo "$IP" | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}" if there is enough testing of four three-digit numbers through a point, without checking for exceeding 255 and validity and compliance with the "white" - "gray" ranges.
You can also make whois "$IP" , in Russian localization, to an invalid address, whois always returns:
Нет whois-сервера для объектов данного вида. this works for both local addresses and domain names.
a fairly correct expression for checking ipv4-addresses:
$ echo "ipv4-адрес" | grep -Eq '^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?)$'
does not miss:
1.2.3.0 0.0.0.256 xyzt 1...1 1.1.1 1.1.1.1.1 1000.1.1.1 1.2.3.04 и т.п. so that the addresses, in the last octet of which zero is written (see the discussion in the comments), should be skipped, should the last one be passed ? slightly to the left:
$ echo "ipv4-адрес" | grep -Eq '^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$'
explanation:
-Eis a grep program option that enables extended regexp mode. in this case it is used only in order not to overload the expression with backslashes-q- option of the grep program, thanks to which the program will not write anything to stdout , but will only return the return code (there is a match - it will return 0, otherwise - it will return non-zero)^...$- binding to the beginning and end of the line(...)- group(...){3}- the group should repeat exactly three times.(выражение1|выражение2|...)- must match eitherвыражение1orвыражение2or...[...]- any of the listed characters ("set")[0-5]- any of the numbers from zero to fivex?- the characterxcan occur zero or once (in this case, the “character” can be both a group(...)and a character set[...]\.- the “dot” character (without a backslash, the dot is a quantifier that matches any (one) character)
- sure that 1.2.3.0 is not a valid ip-address? - ArcherGodson
- @ArcherGodson
whois 1.2.3.0>> For details, refer to the APNIC Whois Database via WHOIS.APNIC.NET or wq.apnic.net/apnic-bin/whois.pl " - Nick Volynkin ♦ - # whois 1.2.3.0% [whois.apnic.net]% Whois data copyright terms apnic.net/db/dbcopyright.html% Information related to '1.2.3.0 - 1.2.3.255'% Abuse contact for '1.2.3.0 - 1.2 .3.255 'is' abuse@apnic.net' inetnum: 1.2.3.0 - 1.2.3.255 netname: Debogon-prefix descr: APNIC Debogon Project descr: APNIC Pty Ltd ... change huiz - ArcherGodson
- Can I go on the other hand, 192.168.100.0 is not exactly valid? and 192.168.1.1? - ArcherGodson
- @ArcherGodson,
1.2.3.0and192.168.100.0- yes, I think they are invalid for a host.192.168.1.1- thanks, corrected typo. - aleksandr barakin
You can make sure that the IP-address is recorded correctly, you can use such a regular schedule ( taken from here ). This expression allows both IPv4 and IPv6 addresses.
(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))|((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]) Here is an example script for the command interpreter.
ip_regex='(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))|((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])' if echo "$param" | egrep -q "$ip_regex"; then // теперь в переменной $param что-то похожее на IP-адрес ... fi rexp='(?:(?:\d{1,2}|1\d{,2}|2[0-4]?\d?|25?[0-5]?)\.){3}(?:\d{1,2}|1\d{,2}|2[0-4]?\d?|25?[0-5]?)'
man ipcalc,man ipv6calc. - 0andriy