Re: [Pyparsing] parsing and extracting tac_plus.conf user data
Brought to you by:
ptmcg
From: Asif I. <va...@gm...> - 2014-05-25 05:42:35
|
On Sun, May 25, 2014 at 1:07 AM, Asif Iqbal <va...@gm...> wrote: > Hi All, > > I am trying to use pyparse to extract user data and it only picks up the > first block. > > Any idea what I am doing wrong? I am using python 2.7.6 on ubuntu trusty > > #!/usr/bin/python > import pprint, sys > #from pyparsing import Word, Literal, Forward, Group, ZeroOrMore, alphas > from pyparsing import * > > f = sys.argv[1] > > data = open(f,'r').read() > > nestedCurlies = nestedExpr('{','}') > > expr = Word(alphas) + '=' + Word(alphanums) + Optional(nestedCurlies) > > expr.ignore("#" + restOfLine) > result = expr.parseString(data).asList() > using scanString was the trick. result = list(expr.scanString(data)) did the trick. > > pprint.pprint(result) > > Here is sample data file: > > user = aa06591 { > pap = PAM > login = PAM > member = readonly > > ## temporary commands so John can adjust resolver > ## uplink speed/duplex on SVCS routers > > cmd = interface { > permit "Ethernet" > deny .* > } > > cmd = speed { > permit .* > } > cmd = duplex { > permit .* > } > cmd = default { > permit speed > permit duplex > deny .* > } > cmd = write { > deny ^erase > permit .* > } > } > user = lukesd { > pap = des 11uGIcdXQ6v9E > login = file /etc/tacacs-passwd > member = readonly > } > user = curryc { > pap = PAM > login = PAM > member = implementation > } > user = rhodesw { > pap = PAM > login = PAM > member = implementation > } > user = aa68442 { > pap = PAM > login = PAM > member = implementation > } > user = jdimayu { > pap = PAM > login = PAM > member = readonly > } > > > Here is the output, and it only displays the first block > > ['user', > '=', > 'aa60591', > ['pap', > '=', > 'PAM', > 'login', > '=', > 'PAM', > 'member', > '=', > 'readonly', > 'cmd', > '=', > 'interface', > ['permit', '"Ethernet"', 'deny', '.*'], > 'cmd', > '=', > 'speed', > ['permit', '.*'], > 'cmd', > '=', > 'duplex', > ['permit', '.*'], > 'cmd', > '=', > 'default', > ['permit', 'speed', 'permit', 'duplex', 'deny', '.*'], > 'cmd', > '=', > 'write', > ['deny', '^erase', 'permit', '.*']]] > > > -- > Asif Iqbal > PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu > A: Because it messes up the order in which people normally read text. > Q: Why is top-posting such a bad thing? > > -- Asif Iqbal PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? |