IP REGEX验证

时间:2011-11-18 03:09:18

标签: python regex validation loops

我一直在尝试验证输入的字符串(在这种情况下为sys argv[1])。我需要创建一个遍历日志文件的脚本,并将源和目标ip的条目与使用脚本输入的任何参数进行匹配。有效输入的种类是

  • IP或部分IP
  • “any”(字符串表示给定列中的所有IP地址)。

到目前为止,我有以下代码。每当我在bash中运行脚本以及参数(例如任何随机数或单词/字母等)时,我都会遇到错误。请告诉我如何解决它们。非常感谢一种根据IP地址reg ex和单词any验证输入的方法。

#!/usr/bin/python

import sys,re

def ipcheck(ip):
    #raw patterns for "any" and "IP":
    ippattern = '([1-2]?[0-9]?[0-9]\.){1,3}([1-2]?[0-9]?[0-9])?'
    anypattern = any
    #Compiled patterns
    cippattern = re.compile(ippattern)
    canypattern = re.compile(any)
    #creating global variables for call outside function    
    global matchip
    global matchany
    #matching the compiled pattern 
    matchip = cippattern.match(ip)
    matchany = canypattern.match(ip)

new = sys.argv[1]
snew = str(new)
print type(snew)
ipcheck(new)

我也尝试这样做,但它一直给我错误,是否可以通过“OR |”将2个参数传递给if循环运营商?我怎么这样做?[/ b]

#if (matchip | matchany) :  
    #print "the ip address is valid"
#else:
    #print "Invalid Destination IP"


Error
========================

user@bt:/home# ./ipregex.py a
<type 'str'>
Traceback (most recent call last):
File "./ipregex.py", line 21, in <module>
ipcheck(new)
File "./ipregex.py", line 15, in ipcheck
matchany = re.match(anypattern,ip)
File "/usr/lib/python2.5/re.py", line 137, in match
return _compile(pattern, flags).match(string)
File "/usr/lib/python2.5/re.py", line 237, in _compile
raise TypeError, "first argument must be string or compiled pattern"
TypeError: first argument must be string or compiled pattern

=============================================== ===========

修改

我试图匹配IP而不编译正则表达式。所以我修改了脚本来这样做。这导致了错误:

错误

user@bt:/home# ./ipregex.py a
<type 'str'>
Traceback (most recent call last):
File "./ipregex.py", line 21, in <module>
ipcheck(new)
File "./ipregex.py", line 15, in ipcheck
matchany = anypattern.match(ip)
AttributeError: 'builtin_function_or_method' object has no attribute 'match'

=============================================== ===========

EDIT#2

我能够在更简单的代码版本中重现我的错误。我到底做错了什么??????

#!/usr/bin/python

import sys
import re

def ipcheck(ip):
    anypattern = any
    cpattern = re.compile(anypattern)
    global matchany
    matchany = cpattern.match(ip)
    if matchany:
            print "ip match: %s" % matchany.group()
new = sys.argv[1]
ipcheck(new)

错误

user@bt:/home# ./test.py any
Traceback (most recent call last):
File "./test.py", line 14, in <module>
ipcheck(new)
File "./test.py", line 8, in ipcheck
cpattern = re.compile(anypattern)
File "/usr/lib/python2.5/re.py", line 188, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.5/re.py", line 237, in _compile
raise TypeError, "first argument must be string or compiled pattern"
TypeError: first argument must be string or compiled pattern

2 个答案:

答案 0 :(得分:1)

使用re.compile时,在编译对象上调用match函数:ippattern.match(ip)。另外,要从MatchObject获取匹配的IP,请使用MatchObject.group()。修正了你的例子,它现在应该做你需要的:

#!/usr/bin/python

import sys
import re

def ipcheck(ip):
    ippattern_str = '(([1-2]?[\d]{0,2}\.){1,3}([1-2]?[\d]{0,2})|any)'

    ippattern = re.compile(ippattern_str)
    # ippattern is now used to call match, passing only the ip string
    matchip = ippattern.match(ip)
    if matchip:
        print "ip match: %s" % matchip.group()

if len(sys.argv) > 1:
    ipcheck(sys.argv[1])

一些结果:

[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.
ip match: 100.
[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.
ip match: 100.1.
[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.55.
ip match: 100.1.55.
[ 19:46 jon@hozbox ~/SO/python ]$ ./new.py 100.1.55.255
ip match: 100.1.55.255
[ 19:47 jon@hozbox ~/SO/python ]$ ./new.py any
ip match: any
[ 19:47 jon@hozbox ~/SO/python ]$ ./new.py foo
[ 19:47 jon@hozbox ~/SO/python ]$ 

答案 1 :(得分:0)

这个正则表达式可能更好:

((([1-2]?[0-9]?[0-9]\.){1,3}([1-2]?[0-9]?[0-9])?)|any)

它将匹配以下内容:

127.0.0.1
127.0.0
127.0
127.
192.168.1.1
any

您的正则表达式会遇到上述问题,因为它与0不匹配。

编辑:

我错过了关于匹配any的部分。

此正则表达式将匹配一些无效地址,但是如果您只是搜索应该没问题的日志文件。如果您确实需要准确,可以查看this link