Use subprocess
:
import subprocess
print(subprocess.check_output(['nslookup', 'google.com']))
If the return code is not zero it will raise a CalledProcessError
exception:
try:
print(subprocess.check_output(['nslookup', 'google.com']))
except subprocess.CalledProcessError as err:
print(err)
os.system only returns the exit code of the command. Here 0
means success. Any other number stands for an operating-system-dependent error. The output goes to stdout of this process. subprocess intends to replace os.system
.
subprocess.check_output is a convenience wrapper around subprocess.Popen that simplifies your use case.