Есть ли способ объединить переменные Python в os.system ()? - PullRequest
0 голосов
/ 08 мая 2019

Я пытаюсь объединить переменные python в os.system, команда, кажется, выполняется, но не принимает правильно выделенное значение.

Я пытался использовать os.system и subprocess, но ни один из них не работает. Вот некоторые из моих попыток.

interface = os.popen("netstat -i | awk '$1 ~ /^w/ {print $1}'")
os.system("iw dev %s station dump" % (interface))

.

interface = os.popen("netstat -i | awk '$1 ~ /^w/ {print $1}'")
os.system("iw dev" +interface+ "station dump")

.

p1 = subprocess.Popen(["netstat", "-i"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["awk", '$1 ~ /^w/ {print $1}'], stdin=p1.stdout, 
stdout=subprocess.PIPE)

displayInterface = p2.communicate()[0].decode('ascii')
retrieveMac = subprocess.Popen(["iw", "dev", displayInterface, "station", "dump"])

1 Ответ

0 голосов
/ 09 мая 2019

В этой строке:

displayInterface = p2.communicate()[0].decode('ascii')

displayInterface приводит к строке с завершающим переводом строки. Я не знаю, нужен ли вам decode(), но вам нужно раздеть перевод строки.

displayInterface = p2.communicate()[0].rstrip()

Вы можете указать символ (ы) для разбивки в аргументе до rstrip(), если необходимо.

...