Python 专用IP地址的正则表达式

Python 专用IP地址的正则表达式,python,regex,Python,Regex,我不熟悉编码和python,我想知道如何创建一个正则表达式来匹配所有以192.168.1.xxx开头的ip地址。我一直在网上寻找,但还没有找到匹配的。下面是一些示例数据,我正试图从中匹配它们 /index.html HTTP/1.1" 404 208 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0" 192.168.1.142 - -

我不熟悉编码和python,我想知道如何创建一个正则表达式来匹配所有以192.168.1.xxx开头的ip地址。我一直在网上寻找,但还没有找到匹配的。下面是一些示例数据,我正试图从中匹配它们

 /index.html HTTP/1.1" 404 208 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:53 -0400] "GET /first HTTP/1.1" 404 203 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET /HNAP1/ HTTP/1.1" 404 204 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET / HTTP/1.1" 403 4897 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ HTTP/1.1" 404 203 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ HTTP/1.1" 404 203 "-" "-"

给你。还有结帐


^192\.168\.1\[0-9]{1,3}$
给你。还有结帐


^192\.168\.1\[0-9]{1,3}$

如果您真的只想匹配'192.168.1.xxx',那么您可以使用这个正则表达式在python中专门使用它:“192\.168\.1\[0-9]{1,3}”


我个人建议使用来更加熟悉正则表达式。你可以输入你的数据,在左边你可以查看一个备忘表来帮助你学习。

如果你真的只想匹配'192.168.1.xxx',那么你可以使用这个正则表达式在python中特别使用它:“192\.168\.1\.[0-9]{1,3}”


我个人建议使用来更加熟悉正则表达式。你可以输入你的数据,在左边你可以查看一个备忘单来帮助你学习。

我认为在这里最好结合使用
regex
从你的数据中逐行获取任何有效的IP地址。然后使用
ipaddress
检查地址是否位于您要查找的网络中

如果您需要检查不同的网络,这将提供更大的灵活性,而不是每次都重写
regex
,您可以创建一个
ip_网络
对象。我们还可以创建多个网络,并检查它们是否存在

import ipaddress
import re

data =  '''/index.html HTTP/1.1" 404 208 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:53 -0400] "GET /first HTTP/1.1" 404 203 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET /HNAP1/ HTTP/1.1" 404 204 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET / HTTP/1.1" 403 4897 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ HTTP/1.1" 404 203 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ HTTP/1.1" 404 203 "-" "-"'''

network = ipaddress.ip_network('192.168.1.0/24')

# Pattern that matches any valid ipv4 address
pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

for row in data.split():
    if (ip := re.search(pattern, row)):
        if ipaddress.IPv4Address(ip.group()) in network:
            print(f'{ip.group()} exists in {network}')

输出

192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24

我认为这里最好结合使用
regex
从数据中逐行获取任何有效的IP地址。然后使用
ipaddress
检查地址是否位于您要查找的网络中

如果您需要检查不同的网络,这将提供更大的灵活性,而不是每次都重写
regex
,您可以创建一个
ip_网络
对象。我们还可以创建多个网络,并检查它们是否存在

import ipaddress
import re

data =  '''/index.html HTTP/1.1" 404 208 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:43 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://optiplex360/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.142 - - [30/Sep/2016:16:18:53 -0400] "GET /first HTTP/1.1" 404 203 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET /HNAP1/ HTTP/1.1" 404 204 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "GET / HTTP/1.1" 403 4897 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ HTTP/1.1" 404 203 "-" "-"
192.168.1.1 - - [30/Sep/2016:16:19:00 -0400] "POST /JNAP/ HTTP/1.1" 404 203 "-" "-"'''

network = ipaddress.ip_network('192.168.1.0/24')

# Pattern that matches any valid ipv4 address
pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

for row in data.split():
    if (ip := re.search(pattern, row)):
        if ipaddress.IPv4Address(ip.group()) in network:
            print(f'{ip.group()} exists in {network}')

输出

192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.142 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24
192.168.1.1 exists in 192.168.1.0/24

你试过什么?这是一项容易的任务。事实上,您可以通过对“192.168.1”进行简单的非正则搜索来定位这些地址。顺便说一句,您知道私有IP包括“192.168.x.x”中的所有内容,对吧?公平地说@TimRoberts私有IP是
10.0.0.0/8
172.16.0.0/16
192.168.0.0/24
。标题有点误导人。我们正在寻找一个正则表达式来匹配任何私人地址吗?如果我们很挑剔,范围是
10.0.0/8
172.16.0.0/12
192.168.0.0/16
。您尝试了什么?这是一项容易的任务。事实上,您可以通过对“192.168.1”进行简单的非正则搜索来定位这些地址。顺便说一句,您知道私有IP包括“192.168.x.x”中的所有内容,对吧?公平地说@TimRoberts私有IP是
10.0.0.0/8
172.16.0.0/16
192.168.0.0/24
。标题有点误导人。我们在寻找一个正则表达式来匹配任何私有地址吗?如果我们很挑剔,范围是
10.0.0/8
172.16.0.0/12
192.168.0.0/16
。谢谢,我肯定会使用这个。问题提到Python,但这不是Python语法-斜杠和“g”可能会让新用户感到困惑。你是对的,我掩盖了这一点。我自己更新了答案并用python测试了它,以防万一,谢谢,我肯定会使用它。问题提到python,但这不是python语法-斜杠和“g”可能会让新用户感到困惑。你说得对,我掩盖了这一点。为了以防万一,我自己更新了答案并用python进行了测试