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.
os.system("ls")only returns the exit_code forlswhich is a unix integer status from the operating system for the process. 0 here means "no-error". Both the stdout and stderr of os.system is piped into your python program's stdout and stderr. So either you re-implement this stdout redirection manually, or use a different python function that does this work automatically for you, one example of many beingsubprocess.