방화벽 구성 파일에 대한 파서를 쓰고 있습니다. PyParsing과 Python을 처음 접해 보았습니다.다른 문자열 길이를 PyParsing
질문은 어떻게 3 개 이상의 인수가 발생하면 구문 분석합니까 (xxxx, xxxx, xxxx)! = (xxxx, xxxx, xxxx, xxxx), 모든 규칙이 올바르게 작동하고 각 행에 더 이상 3 개의 문자열은 아니지만 [방화벽 [F1]]에는 주소 필드 뒤에 "NAT"가 포함되어 있으며 규칙을 변경하는 방법과 상관없이 무시됩니다.
(DEF printTokens (S, LOC, toks) : #의 S = 오리지널 캐릭터, LOC = 위치 toks = 매칭 토큰)를 사용
넷째 인자 ("NAT")를 사용하는 경우 (2 개)의 출력을 참조하세요 우리가 지울 때. 미리 감사드립니다! 규칙이 구현 된 "NAT"를 포함한 모든 것을 구문 분석해야합니다.
from pyparsing import *
#===================================GRAMMER==================================
zone = Literal("zone")
zoneid = Word(alphanums)
host = Literal("host")
hostid = Word(alphanums)
interface = Literal("interface")
interfaceid = Word(alphanums)
firewall = Literal("firewall")
firewallid = Word(alphanums)
router = Literal("router")
routerid = Word(alphanums)
fstop = Literal(".")
comma = Suppress(",") #Converter for ignoring the results of a parsed expression.
slash = Literal("/")
ocbracket = Literal("{")
ccbracket = Literal("}")
sobracket = Literal("[")
scbracket = Literal("]")
hyphen = Literal("-")
underline = Literal("_")
word = Word(alphas)
#===================================IP-TYPE=================================
ip=Combine(Word(nums)+
fstop+ Word(nums) +
fstop+ Word(nums) +
fstop + Word(nums))
subnet = Combine(slash +Word(nums))
address = ip + Optional(subnet)
#===================================RULES===================================
#adword = address + word
zoneRule = zone + zoneid + address
hostRule = host + hostid + ocbracket
interfaceRule = interface + interfaceid + address
interfaceRule2 = interface + interfaceid + address + word
firewallRule = firewall + firewallid + ocbracket
routerRule = router + routerid + ocbracket
endRule = ccbracket
rule = zoneRule | hostRule | interfaceRule | interfaceRule2 | firewallRule | routerRule | endRule
rules = OneOrMore(rule)
#===================================DATA=====================================
details = """zone zone1 10.1.0.0/24
zone backbone 10.254.0.0/24
zone zone 10.2.0.0/24
host ha {
interface iha 10.1.0.1
}
host hb {
interface ihb 10.2.0.1
}
firewall f1 {
interface ifla 10.1.0.254
interface iflback 10.254.0.101 nat
}
router r2 {
interface ir2back 10.254.0.102
}
router r3 {
interface ir3b 10.2.0.103
}"""
#==================================METHODS==================================
def printTokens(s,loc,toks): #s=orig string, loc=location, toks=matched tokens
print (toks)
zoneRule.setParseAction(printTokens)
hostRule.setParseAction(printTokens)
interfaceRule.setParseAction(printTokens)
interfaceRule2.setParseAction(printTokens) #takes in 4 instances where as 3 declared
firewallRule.setParseAction(printTokens)
routerRule.setParseAction(printTokens)
endRule.setParseAction(printTokens)
rules.parseString(details)
#================================OUTPUT RESULT WITH NAT=================================
"""
['zone', 'zone1', '10.1.0.0', '/24']
['zone', 'backbone', '10.254.0.0', '/24']
['zone', 'zone', '10.2.0.0', '/24']
['host', 'ha', '{']
['interface', 'iha', '10.1.0.1']
['}']
['host', 'hb', '{']
['interface', 'ihb', '10.2.0.1']
['}']
['firewall', 'f1', '{']
['interface', 'ifla', '10.1.0.254']
['interface', 'iflback', '10.254.0.101']"""
#================================OUTPUT RESULT WITHOUT NAT=================================
"""['zone', 'zone1', '10.1.0.0', '/24']
['zone', 'backbone', '10.254.0.0', '/24']
['zone', 'zone', '10.2.0.0', '/24']
['host', 'ha', '{']
['interface', 'iha', '10.1.0.1']
['}']
['host', 'hb', '{']
['interface', 'ihb', '10.2.0.1']
['}']
['firewall', 'f1', '{']
['interface', 'ifla', '10.1.0.254']
['interface', 'iflback', '10.254.0.101']
['}']
['router', 'r2', '{']
['interface', 'ir2back', '10.254.0.102']
['}']
['router', 'r3', '{']
['interface', 'ir3b', '10.2.0.103']
['}']"""
'규칙 = zoneRule | hostRule | interfaceRule | interfaceRule2 | firewallRule | routerRule | endRule'을'rule = zoneRule | hostRule | interfaceRule2 | interfaceRule | firewallRule | routerRule | endRule'. 또한 가능한 경우'interfaceRule2 = interface + interfaceid + address + CaselessLiteral ('nat')'또는'interfaceRule2 = interface + interfaceid + address + oneOf ("nat ext ipv6 other1 other2")와 같이' '. 또한 IPv4 및 IPv6 주소에 대한'pyparsing_common' 정의를 포함하여 2.1.5 릴리스를 살펴보십시오. – PaulMcG