Skip to content Skip to sidebar Skip to footer

Why Doesnt This Regex Work?

Here is the code in question: import subprocess import re import os p = subprocess.Popen(['nc -zv 8.8.8.8 53'], stdout=subprocess.PIPE, shell = True) out, err = p.communicate() re

Solution 1:

The output is coming out stderr not stdout:

stderr=subprocess.PIPE

You can simplify to using in and you don't need shell=True:

p = subprocess.Popen(["nc", "-zv", "8.8.8.8", "53"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

if"succeeded"notin err:
    print ("test")

You can also redirect stderr to STDOUT and use check_output presuming you are using python >= 2.7:

out = subprocess.check_output(["nc", "-zv", "8.8.8.8", "53"],stderr=subprocess.STDOUT)

if"succeeded"notinout:
    print ("test")

Post a Comment for "Why Doesnt This Regex Work?"