Discussion:
[Pyparsing] parsing and extracting tac_plus.conf user data
Asif Iqbal
2014-05-25 05:07:41 UTC
Permalink
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?
Asif Iqbal
2014-05-25 05:42:06 UTC
Permalink
Post by Asif Iqbal
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.
Post by Asif Iqbal
pprint.pprint(result)
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?
Paul McGuire
2014-05-25 05:46:30 UTC
Permalink
Asif Iqbal
2014-05-25 06:59:58 UTC
Permalink
Post by Asif Iqbal [mailto:]
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
Post by Asif Iqbal [mailto:]
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
expr = Group(Word(alphas) + '=' + Word(alphanums) +
Optional(nestedCurlies))
-- Paul
-----Original Message-----
Sent: Sunday, May 25, 2014 12:08 AM
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)
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
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
2014-05-25 07:02:26 UTC
Permalink
Post by Asif Iqbal
Post by Asif Iqbal [mailto:]
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))
Post by Asif Iqbal
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
Post by Asif Iqbal [mailto:]
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
expr = Group(Word(alphas) + '=' + Word(alphanums) +
Optional(nestedCurlies))
-- Paul
-----Original Message-----
Sent: Sunday, May 25, 2014 12:08 AM
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)
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
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?
Asif Iqbal [mailto:]
1970-01-01 00:00:00 UTC
Permalink
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:***@gmail.com]
Sent: Sunday, May 25, 2014 12:08 AM
To: pyparsing-***@lists.sourceforge.net
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
Pyparsing-***@lists.sourceforge.net
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
Loading...