简体   繁体   中英

Does Pythons re.compile need different syntax than re.search?

I'm playing around with a program that needs to trigger if a certain value is present in output from a command sent via Paramiko, in this case a specific SSID.

The output I get from the command looks like this:

[u'{\n', u"          'autoname' => 0,\n", u"          'class' => 'itfhw',\n", u"          'data' => {\n", u"                      'ap_bridgemode' => 'none',\n", u"                      'bridge' => '',\n", u"                      'client_isolation' => 0,\n", u"        'comment' => '',\n", u"                      'crypto_alg' => 'aes',\n", u"                      'description' => 'Remote Wireless Network',\n", u"                      'dot11r' => 0,\n", u"            'dynamic_vlan' => 0,\n", u"                      'encryption_mode' => 'wpa2_personal',\n", u"                      'freq_bands' => 'a',\n", u"                      'hardware' => 'wlan1',\n", u"                  'hide_ssid' => 0,\n", u"                      'interface_name' => 'wifi1',\n", u"                      'mac' => '00:1a:8c:0a:73:01',\n", u"                      'mac_filter' => 'disable',\n", u"              'mac_list' => '',\n", u"                      'mesh_id' => '',\n", u"  'mesh_mode' => 'none',\n", u"                      'mesh_subtag' => '',\n", u"                      'name' => 'wlan1 (Remote Wireless Network)',\n", u"                      'network_mode' => 'mixed_bgn',\n", u"                      'network_name' => 'Test1',\n", u"                      'psk' => 'secretpw',\n", u"       'r0kh_secret' => 'o2ZT4VYEYB7hhlfQnHmJQONGYnvY12',\n", u"              'ssid' => 'HACKME',\n", u"                      'ssid_vlantag' => '',\n", u"                      'status' => 1,\n", u"                  'time_scheduling' => 0,\n", u"                      'time_select' => [],\n", u"                      'utf8_ssid' => 1,\n", u"               'vlantag' => 101,\n", u"                      'wep128' => '',\n", u"   'wep_authentication' => 'open'\n", u'                    },\n', u"     'hidden' => 0,\n", u"          'lock' => '',\n", u"          'nodel'
=> '',\n", u"          'ref' => 'REF_ItfAweTest1',\n", u"          'type' => 'awe_network'\n", u'        }\n']

And that is what I need to search for the ssid name, in this case HACKME in order to trigger the next part of the program. That part looks like this:

'ssid' => 'HACKME',\n", u"

If I use the following code

from re import search as re_search
ssid = 'HACKME'
#lots of guff removed
if not re_search('\'ssid\' =\> \'' + ssid +'\'', str(stdout.readlines())):
    continue
else:
    print 'SSID found - let's do something

It all works. However, if I instead use this:

import re
ssid = 'HACKME'
# lots of guff removed
ssidRegex = re.compile('\'ssid\' => \'' + ssid +'\'')
ssidresult = ssidRegex.search(str(stdout.readlines))
if not ssidresult:
    continue
else:
    print 'SSID found - let's do something'

I can run both these after each other on the same output but it will only be triggered on the 'non compiled' version of it. Which is currently driving me mad.

During the execution I've even added a 'print ssidRegex.pattern' to my code and that looks just fine to me. Which must mean that something is horribly wrong. The output looks like this:

'ssid' => 'HACKME'

I'm now hoping that one of you out there will spot my (most likely very) obvious mistake and put me on the right track. I know there's an extra '\\' between the '=' and '>' in the first version of the code but I get errors when I add it to the compile version and I don't seem to be able to escape it out properly.

And yes - I'm aware that compiling the regex might not add much speed to my program, but I want to learn what I'm doing wrong here now. As a pure matter of principle. ;)

The code is missing () after the stdout.readlines .

ssidresult = ssidRegex.search(str(stdout.readlines()))
                                                  ^^

UPDATE

Using read method might be more appropirate, because read will return a string instead of a list of strings; no need to call str :

ssidresult = ssidRegex.search(stdout.read())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM