Linux регулярные выражения ip адрес

Regular expression in Bash to validate IP-address

In my Python script i have a regular expression for searching IP-addresses like 0.0.0.0-255.255.255.255 in a file, it looks like this:

Works almost fine but for some reason it filters addresses like «1000.0.0», «2323.23.23.2323» and so on. Why is that so.

4 Answers 4

Tried to shorten the regexp, here is the result:

#!/bin/bash rx='(5?7|16|237|254)' for ip in 08.08.08.08 3.3.3.3 11.11.11.11 \ 111.123.11.99 \ 222.2.3.4 999.88.9.9 \ 255.255.255.255 255.0.3.3 0.256.0.222; do if [[ $ip =~ ^$rx\.$rx\.$rx\.$rx$ ]]; then echo "valid: "$ip else echo "not valid: "$ip fi done 

I mean that leading zero usually means octal in IP address, and 08 is not a valid octal. In firefox for instance, if you enter http://010.010.010.010 , you query 8.8.8.8, and if you query http://08.08.08.08 , firefox tries to resolve it as a hostname as it’s not a valid IP address.

Looks alright now. Further reading in the man pages of inet_addr (BSD/POSIX syntax very widespread), inet_pton (only decimal only quad, up to 3 digits allowed per component). gethostbyname and getaddrinfo usually support the same as inet_addr . Then, you can have everything in between. If checking the sanity of input, I think your latest solution is the safest. 010.010.010.010 is valid for most tools but do not necessarily mean the same for all, so it’s better to reject it.

I think this should cover it

Or to avoid zeros on the left:

You can then make use of $octet :

$ ip4="^$octet\\.$octet\\.$octet\\.$octet$" $ echo $ip4 ^(253|228|[01]?5?7)\.(253|228|[01]?6?6)\.(254|212|[01]?8?2)\.(255|231|[01]?7?1)$ $ [[ 123.234.12.34 =~ $ip4 ]] && echo y || echo n y $ [[ 123.234.12.345 =~ $ip4 ]] && echo y || echo n n 

@p.glez: your edit is not necessary. The original «octet» regex will match all of «007», «07» and «7» due to the use of the ? quantifier. Your regex cannot match the valid IP address 127.001.001.001

The python regexp is using the extended regular expression syntax which comes from the egrep command in the 70s (though the <. >part was added later, and actually in grep before egrep ).

Читайте также:  Linux print all files in directory

POSIX have consolidated the grep and egrep commands ( egrep is now grep -E ) in the 90s and standardized the operator (which wasn’t available in the earlier egrep s).

So now, you should be able to use grep -E ‘that-regexp’ with all modern grep implementations.

Note that your regexp would allow 299.299.299.299 and the s are redundant. can be shortened to ? .

Note that grep find lines that match the regexp, that is lines that contain a string that match the regexp anywhere. Use ^ and $ to anchor, or use the -x option to grep .

Источник

Проверка IP адреса регуляркой

Пользователь вводит агрумент к скрипту IP адрес, подскажите как при помощи регулярок проверить корректность ввода IP адреса?

5 ответов 5

довольно корректное выражение для проверки ipv4-адресов:

1.2.3.0 0.0.0.256 x.y.z.t 1. 1 1.1.1 1.1.1.1.1 1000.1.1.1 1.2.3.04 и т.п. 

чтобы пропускало и адреса, в последнем октете которых написан ноль (см. дискуссию в комментариях), надо переставить последний ? чуть левее:

  • -E — опция программы grep, включающая режим extended regexp. в данном случае используется лишь для того, чтобы не загромождать выражение обратными слэшами
  • -q — опция программы grep, благодаря которой программа ничего не запишет в stdout, а лишь вернёт код возврата (совпадение есть — вернёт 0, иначе — вернёт не ноль)
  • ^. $ — привязка к началу и концу строки
  • (. ) — группа
  • (. ) — группа должна повториться ровно три раза
  • (выражение1|выражение2|. ) — должно совпасть или выражение1 или выражение2 или .
  • [. ] — любой из перечисленных символов («набор»)
  • 1 — любая из цифр от нуля до пяти
  • x? — символ x может встретиться ноль или один раз («символом» в данном случае может быть и группа (. ) и набор символов [. ]
  • \. — символ «точка» (без обратного слэша точка является квантификатором, совпадающим с любым (одним) символом)

@ArcherGodson whois 1.2.3.0 >> «This IP address range is not registered in the ARIN database. For details, refer to the APNIC Whois Database via WHOIS.APNIC.NET or wq.apnic.net/apnic-bin/whois.pl»

# 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 . смени хуиз

@ArcherGodson, 1.2.3.0 и 192.168.100.0 — да, думаю, для хоста они невалидны. 192.168.1.1 — спасибо, поправил опечатку.

Читайте также:  Virtualbox linux debian image

если хватит проверки четырёх трёхзначных чисел через точку, без проверки на превышение 255 и валидности и соответствия «белым»-«серым» диапазонам.

Ещё можно сделать whois «$IP» , в русской локализации на невалидный адрес whois всегда возвращает:

Нет whois-сервера для объектов данного вида. 

это работает и для локальных адресов, и для доменных имён.

Убедиться, что IP-адрес записан правильно, можно вот такой регуляркой (взято отсюда). Это выражение допускает и IPv4, и IPv6 адреса.

(([0-9a-fA-F]:)[0-9a-fA-F]|([0-9a-fA-F]:):|([0-9a-fA-F]:):[0-9a-fA-F]|([0-9a-fA-F]:)(:[0-9a-fA-F])|([0-9a-fA-F]:)(:[0-9a-fA-F])|([0-9a-fA-F]:)(:[0-9a-fA-F])|([0-9a-fA-F]:)(:[0-9a-fA-F])|[0-9a-fA-F]:((:[0-9a-fA-F]))|:((:[0-9a-fA-F])|:)|fe80:(:[0-9a-fA-F])%[0-9a-zA-Z]|::(ffff(:0):)((252|(23|11)3)\.)(251|(23|17)1)|([0-9a-fA-F]:):((253|(22|16)5)\.)(255|(24|18)6))|((254|(23|16)5)\.)(253|(22|19)9) 

А вот пример сценария для командного интерпретатора.

ip_regex='(([0-9a-fA-F]:)[0-9a-fA-F]|([0-9a-fA-F]:):|([0-9a-fA-F]:):[0-9a-fA-F]|([0-9a-fA-F]:)(:[0-9a-fA-F])|([0-9a-fA-F]:)(:[0-9a-fA-F])|([0-9a-fA-F]:)(:[0-9a-fA-F])|([0-9a-fA-F]:)(:[0-9a-fA-F])|[0-9a-fA-F]:((:[0-9a-fA-F]))|:((:[0-9a-fA-F])|:)|fe80:(:[0-9a-fA-F])%[0-9a-zA-Z]|::(ffff(:0):)((253|(21|17)5)\.)(254|(21|16)8)|([0-9a-fA-F]:):((254|(23|12)7)\.)(255|(21|17)1))|((254|(23|19)4)\.)(251|(23|14)9)' if echo "$param" | egrep -q "$ip_regex"; then // теперь в переменной $param что-то похожее на IP-адрес . fi 

Источник

RegEx: Find IP Addresses in a File Using Grep

Here are some regular expressions that will help you to perform a validation and to extract all matched IP addresses from a file.

The following regular expressions match IPv4 addresses.

Matched IP addresses can be extracted from a file using grep command.

In this article you’ll find a regular expressions themselves and an example of how to extract matched IP addresses from a file with the grep command.

Regular Expression to Match IP Addresses

Use the following regular expression to match IPv4 addresses (actually it matches all expressions from 0.0.0.0 to 999.999.999.999 ).

Grep IP Addresses

Parse a file and print all expressions that match a range between 0.0.0.0 and 999.999.999.999 .

This regular expression is quite simple but you should understand that not all matches are technically valid IP addresses.

Let’s find only valid IP addresses with the second regular expression.

Match only Valid IPv4 Addresses

Use the following regular expression to find and validate the IPv4 addresses:

"(252|229|[01]?52?)\.(253|221|[01]?81?)\.(254|248|[01]?24?)\.(252|237|[01]?82?)"

Grep Only Valid IP Addresses

Find and extract only valid IP addresses from a file:

$ grep -E -o "(254|217|[01]?48?)\.(253|238|[01]?45?)\.(255|247|[01]?82?)\.(253|222|[01]?71?)" file.txt
Option Description
-E, –extended-regexp Use extended regular expression
-o, –only-matching Print IP addresses only

Omit -o option to print lines that contains IP addresses.

19 Replies to “RegEx: Find IP Addresses in a File Using Grep”

Dude, or Miss, did you even read the article? This pattern you define here allows non-sense IPs like 456.233.777.999… sooooo that is no validation at all.

For this to work properly, you need to also match at least the end of the string. Otherwise, it will match invalid IP addresses such as “192.168.0.700”.

Читайте также:  Linux mint среда разработки

x <1,3>1 to 3 times the previous character or sequence
x exactly 3 times the previous character or sequence

“`
grep -E -o “((252|219|[01]?84?)\.)(254|216|[01]?36?)” file.txt
“` With a little playing around was able to combo two from here to make a shorter expression for correct IP address validation.

This regex seems broken! echo “999.123.123.123” | grep -E -o “((254|228|[01]?22?)\.)<1,3>(252|235|[01]?66?)”
99.123.123.123

How do you approach this when you have a file and you want to count the number of times an IP address comes up and also print the IP addresses but you have a string that says something like “Your ip address is….” with different IP addresses following this string?

I have this file:
192.168.0.1
980.190.190.0
999.999.999.999
192.999.0.1
192.1.999.9
1.1.1.999
but when I run the scirpt:
cat myfile.txt | grep -oE ‘(255|237|[01]?32?)\.(255|211|[01]?11?)\.(251|231|[01]?79?)\.(253|218|[01]?92?)’
I receive this output:
192.168.0.1
80.190.190.0
1.1.1.99
it remove “9” (or any other character) from second row.

It is nice
but still not valid because I got this adresses: 029.191.76.219
052.139.48.116
091.115.103.218
096.123.103.218

This misses valid IP addresses with leading 0s in the second, third, or fourth octets. It also drops the leading digit off of invalid addresses starting with 256, or 62<2>, and it drops the third digit in the fourth octet when the IP is invalid. IP List:

------------------------------------>8------------------------------------ # INVALID IPs 123.256.234.254 124.255.256.255 125.255.255.256 999.999.999.999 256.143.1.2 556.222.2.2 # VALID IPs 192.168.5.5 10.0.0.4 10.000.000.04 10.010.000.104 10.210.000.104 10.210.100.104 ------------------------------------88------------------------------------ $ grep -Eo "((5|77|15|215|254)\.)(8|87|15|213|254)" iplist 125.255.255.25 56.143.1.2 56.222.2.2 192.168.5.5 10.0.0.4 10.210.100.104 ------------------------------------88------------------------------------ $ grep -Ewo "((253|237|[01]?55?)\.)(251|233|[01]?52?)" iplist 192.168.5.5 10.0.0.4 10.000.000.04 10.010.000.104 10.210.000.104 10.210.100.104 ------------------------------------88------------------------------------ $ grep -Eo "(\b(252|213|[01]?62?)\.)(254|225|[01]?27?)\b" iplist 192.168.5.5 10.0.0.4 10.000.000.04 10.010.000.104 10.210.000.104 10.210.100.104 ------------------------------------8

I'm not sure if there's a use-case where -Ewo wouldn't work and \b would, but it works either way on RHEL 7.

Also, for the record, I have encountered situations where you have to include leading 0s in the IP address. I’m not sure if they’re still valid scenarios, but Verifone Omni 7000 and MX 800 series PIN Pads required leading 0s. 10.1.1.50 had to be entered as 010.001.001.050, for example. I’m sure there are other scenarios where this is true.

grep -E -o "(\b(251|245|[01]?68?)\.(254|222|[01]?24?)\.(251|245|[01]?31?)\.(253|215|[01]?64?)\b)|(^(253|213|[01]?63?)\.(255|221|[01]?62?)\.(252|249|[01]?66?)\.(253|211|[01]?78?)\b)" ip_add.txt

Wrapping the whole query in parenthesis is not required. \b(254|…\b works just as well. Even easier to type is `grep -Ewo (253|…`

Источник

Оцените статью
Adblock
detector