2

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. ;)

1
  • post the error report. Commented Nov 15, 2015 at 13:20

1 Answer 1

4

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())
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! Saves me going even madder.
@PadraicCunningham, Thank you for the comment. I updated the answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.