views:

40

answers:

1

Is it possible to simulate re.findall in the pexpect module?

I currently have a script that ssh's into a server using pexpect. I then have it send a command to the server which returns a bunch of lines in p.before (p being a pexpect spawn):

JUNK JUNK JUNK IP ADDRESS 10.0.0.1 JUNK JUNK
JUNK IP ADDRESS 10.0.0.3 JUNK JUNK JUNK
JUNK JUNK JUNK
IP ADDRESS 10.0.0.2 JUNK JUNK JUNK JUNK

Note: The ip addresses I used in the example are random ones I used just for this example. In the actual script, it can be any ip address. The information I am trying to find are the ip addresses.

Can pexpect do something like re.findall? Or do I have to do re.findall(regex, p.before)?

A: 

I think you could hack what you're looking for by subclassing searcher_re and using an expect_loop call with an instance of your subclass, which overrides the search method.

In your override, before performing the main search for whatever you're using now, you can do a findall on the buffer for an IP-identifying RE, and accumulate those results in an instance variable of your own. So, when pexpect.expect_loop returns, that variable on your instance will have the "extra info" you're looking for.

Seems like a little bit of trial and error would be needed to get it right, and I don't understand what advantages you expect (by this or any other hack to similar purposes) over the straightforward approach you've mentioned, i.e., just using re.findall directly, without messing with pexpect's machinery -- care to explain...?

Alex Martelli