在 ping 扫描中使用 python 中的 awk

using awk in python in ping sweep

我正在尝试使用子进程在 python 中进行 ping 扫描,但是我在 Popen 中获取 awk 时遇到问题,我想 运行 这个命令:

ping 192.168.1.1 -c1 | grep from | awk '{print "is alive"}'

output should be "assume we can receive reply form it": 192.168.1.1:is alive

这是我的代码"problem is in var p3"

import subprocess
# Ask the user for input for example we will enter 192.168.1.1
host = input("Enter a host to ping: ") 
p1 = subprocess.Popen(['ping', '-c 1', host], stdout=subprocess.PIPE)
p2=subprocess.Popen(["grep","from"],stdin=p1.stdout,stdout=subprocess.PIPE)
p3 = subprocess.Popen(["awk","\'{print  \"is alive\"}\'"], stdin=p2.stdout,stdout=subprocess.PIPE)

# Run the command
output = p3.communicate()[0]
print (output)

去掉单引号。划定shell命令行参数是必须的,没有命令行时不需要。

p3 = subprocess.Popen(["awk", "{print  \"is alive\"}"], ...)