I'm trying to take the output of p5, they are mac address, and I want to store them into a list.
I know that mac addresses are printed in byte type but I can't manage to get them in the type I want.
p3 = subprocess.Popen(["iw", "dev", displayInt, "station", "dump"], stdout=subprocess.PIPE)
p4 = subprocess.Popen(["grep", "Station"], stdin=p3.stdout, stdout=subprocess.PIPE)
p5 = subprocess.Popen(["cut", "-f", "2", "-s", "-d", " "], stdin=p4.stdout, stdout=subprocess.PIPE)
for line in iter(p5.stdout.readline,''):
maclist.append(line.rstrip('\n'))
print(maclist)
I would like to have an output like:
[a1:b2:c3:d4:e5:f6 , a1:b2:c3:d4:e5:f6]
And I'm getting the following error:
TypeError: a bytes-like object is required, not 'str'
It seems you are using Python 3. In Python 3, stdout
is a stream of bytes. If you want to convert that into string, add the encoding='utf8'
paramter to the Popen()
call, for example:
p5 = subprocess.Popen(
["cut", "-f", "2", "-s", "-d", " "],
encoding="utf8",
stdin=p4.stdout,
stdout=subprocess.PIPE)
You might have to include the encoding
parameter for other calls. Also, instead of:
for line in iter(p5.stdout.readline,''):
You might want to try this, which is shorter and easier to understand:
for line in p5.stdout: