Thread: [Pyparsing] parsing and extracting tac_plus.conf user data
Brought to you by:
ptmcg
From: Asif I. <va...@gm...> - 2014-05-25 05:08:10
|
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() 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? |
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? |
From: Paul M. <pt...@au...> - 2014-05-25 05:46:42
|
Pyparsing only reports one because you only told it to get one. Change to: result = OneOrMore(expr).parseString(data).asList() and you'll process the rest of the file too. Also, you will find your results easier to process if you wrap expr in a Group, as in: expr = Group(Word(alphas) + '=' + Word(alphanums) + Optional(nestedCurlies)) -- Paul -----Original Message----- From: Asif Iqbal [mailto:va...@gm...] Sent: Sunday, May 25, 2014 12:08 AM To: pyp...@li... Subject: [Pyparsing] parsing and extracting tac_plus.conf user data 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() 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? ---------------------------------------------------------------------------- -- "Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE Instantly run your Selenium tests across 300+ browser/OS combos. Get unparalleled scalability from the best Selenium testing platform available Simple to use. Nothing to install. Get started now for free." http://p.sf.net/sfu/SauceLabs _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com |
From: Asif I. <va...@gm...> - 2014-05-25 07:00:28
|
On Sun, May 25, 2014 at 1:46 AM, Paul McGuire <pt...@au...> wrote: > Pyparsing only reports one because you only told it to get one. Change to: > > result = OneOrMore(expr).parseString(data).asList() > I am using expr.scanString(data) instead to make sure only to pickup user block like user = foo { .. {..} .. } and not get stuck if sees group = foo {.. {..} .. } Here is a sample of how a real tac_plus.conf looks like and I am only parsing out the user blocks Here are some example tac_plus.conf file https://github.com/mkouhei/tacacs-plus/blob/master/debian/tac_plus.conf https://github.com/mirek186/BackUp/blob/master/tacProject/tac_plus.conf > and you'll process the rest of the file too. > > Also, you will find your results easier to process if you wrap expr in a > Group, as in: > > expr = Group(Word(alphas) + '=' + Word(alphanums) + > Optional(nestedCurlies)) > > -- Paul > > > -----Original Message----- > From: Asif Iqbal [mailto:va...@gm...] > Sent: Sunday, May 25, 2014 12:08 AM > To: pyp...@li... > Subject: [Pyparsing] parsing and extracting tac_plus.conf user data > > 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() > > 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? > > ---------------------------------------------------------------------------- > -- > "Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE > Instantly run your Selenium tests across 300+ browser/OS combos. > Get unparalleled scalability from the best Selenium testing platform > available > Simple to use. Nothing to install. Get started now for free." > http://p.sf.net/sfu/SauceLabs > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > > > --- > This email is free from viruses and malware because avast! Antivirus > protection is active. > http://www.avast.com > > -- 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? |
From: Asif I. <va...@gm...> - 2014-05-25 07:02:54
|
On Sun, May 25, 2014 at 2:59 AM, Asif Iqbal <va...@gm...> wrote: > > > > On Sun, May 25, 2014 at 1:46 AM, Paul McGuire <pt...@au...> wrote: > >> Pyparsing only reports one because you only told it to get one. Change >> to: >> >> result = OneOrMore(expr).parseString(data).asList() >> > > I am using expr.scanString(data) instead to make sure only to pickup > user block like user = foo { .. {..} .. } and not get stuck if sees group > = foo {.. {..} .. } > I am using this code beginWith = Literal('user') identifier = Word(alphas, alphanums) expr = Group(beginWith + '=' + identifier + Optional(nestedCurlies)) > Here is a sample of how a real tac_plus.conf looks like and I am only > parsing > out the user blocks > > Here are some example tac_plus.conf file > > https://github.com/mkouhei/tacacs-plus/blob/master/debian/tac_plus.conf > > https://github.com/mirek186/BackUp/blob/master/tacProject/tac_plus.conf > > > >> and you'll process the rest of the file too. >> >> Also, you will find your results easier to process if you wrap expr in a >> Group, as in: >> >> expr = Group(Word(alphas) + '=' + Word(alphanums) + >> Optional(nestedCurlies)) >> >> -- Paul >> >> >> -----Original Message----- >> From: Asif Iqbal [mailto:va...@gm...] >> Sent: Sunday, May 25, 2014 12:08 AM >> To: pyp...@li... >> Subject: [Pyparsing] parsing and extracting tac_plus.conf user data >> >> 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() >> >> 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? >> >> ---------------------------------------------------------------------------- >> -- >> "Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE >> Instantly run your Selenium tests across 300+ browser/OS combos. >> Get unparalleled scalability from the best Selenium testing platform >> available >> Simple to use. Nothing to install. Get started now for free." >> http://p.sf.net/sfu/SauceLabs >> _______________________________________________ >> Pyparsing-users mailing list >> Pyp...@li... >> https://lists.sourceforge.net/lists/listinfo/pyparsing-users >> >> >> --- >> This email is free from viruses and malware because avast! Antivirus >> protection is active. >> http://www.avast.com >> >> > > > -- > 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? |