-1

I try to execute linux command in Python, but I don't get any output and any error.

import subprocess, os
cmd = ["iwconfig", "wlan0", "|", "grep", "ESSID", "|", "awk", "-F:", "'{print $2}'", "|", "sed", "'s/\"//g'"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = proc.communicate()
print(output.decode("ascii"))

The output should be the ESSID of connected Wi-Fi.

I am sorry, I read the other questions, but I can't get it.

John Doe
  • 7
  • 2

1 Answers1

2

| is a shell feature. You need shell=True.

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895