user5740843
user5740843

Reputation: 1620

Python not putting out actual result

I'm currently compiling a list with only the ESSID of all available networks in the area to be used for another purpose.

This is the script:

from subprocess import check_output
scanoutput = check_output(["iwlist", "wlan0", "scan"])

for line in scanoutput.split():
        if line.startswith("ESSID:"):
                line=line[7:-1]
                print line

When I run the command "sudo iwlist wlan0 scan | grep ESSID" from the SSH, this is the output:

            ESSID:"easybell DSL"
            ESSID:"FRITZ!Box Fon WLAN 7360 SL_EXT"
            ESSID:"NoTrespassing"
            ESSID:"WLAN-519293"
            ESSID:"Cinque"
            ESSID:"EasyBox-738461"
            ESSID:"FRITZ!Box WLAN 3270"
            ESSID:"EasyBox-A2B246"

Sadly, the Python script outputs:

easybel
FRITZ!Bo
NoTrespassing
WLAN-519293
Cinque
EasyBox-738461
FRITZ!Bo
EasyBox-A2B246

With a closer inspection it looks like it borks out when there is a space, but it also seems to remove one last letter before the space before printing it. Can someone explain what is going on here?

As far as I can tell is from the code

line=line[7:-1]

is the 7 responsible to remove the first 7 characters, thus: ESSID:" and the -1 is responsible from the last " thus resulting in a clean line with just the SSID name.

When I run the code without this line this is the output:

ESSID:"easybell
ESSID:"NoTrespassing"
ESSID:"WLAN-519293"
ESSID:"Cinque"
ESSID:"EasyBox-738461"
ESSID:"FRITZ!Box
ESSID:"FRITZ!Box

How is it possible that a from the SSH line I can get the proper information, but when I have python run the code it looses parts?

Any way to fix this?

Thanks!

Upvotes: 0

Views: 74

Answers (3)

jDo
jDo

Reputation: 4010

This won't remove the last character (only the starting/ending quotes). Doing line = line.strip() ensures that startswith("ESSID:") won't fail because of white-space before "ESSID:".

from subprocess import check_output
scanoutput = check_output(["iwlist", "wlan0", "scan"])
for line in scanoutput.split("\n"):
    line = line.strip()
    if line.startswith("ESSID:"):
        line=line[7:-1]
        print line

Upvotes: 1

Cyrbil
Cyrbil

Reputation: 6478

split() by default cuts on any whitespace, you need to explicitly set the split() separator to \n:

string.split(s[, sep[, maxsplit]]): If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed)

>>> test = '''
ESSID:"easybell DSL"
ESSID:"FRITZ!Box Fon WLAN 7360 SL_EXT"
ESSID:"NoTrespassing"
[...]
'''
>>> for l in test.split():
...     if l.startswith('ESSID'):
...         print(l[7:-1])
...         
easybel
FRITZ!Bo
NoTrespassing
[...]
>>> for l in test.split('\n'):
...     if l.startswith('ESSID'):
...         print(l[7:-1])
...         
easybell DSL
FRITZ!Box Fon WLAN 7360 SL_EXT
NoTrespassing
[...]

Or use splitlines():

>>> for l in test.splitlines():
...     if l.startswith('ESSID'):
...         print(l[7:-1])
...         
easybell DSL
FRITZ!Box Fon WLAN 7360 SL_EXT
NoTrespassing
[...]

Upvotes: 2

jwodder
jwodder

Reputation: 57470

You used split() on the output, which splits on any whitespace (not just line breaks), hence line is actually getting set to ESSID:"easybell and then DSL" and then ESSID:"FRITZ!Box, etc. Note that as the final " is not present in the same word as ESSID, line[7:-1] ends up cutting off the last character in ESSID:"easybell, so you end up with easybel. What you want to do instead is for line in scanoutput.splitlines():.

Upvotes: 1

Related Questions