我想从我的系统中检查DNS值

2024-04-25 00:12:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从我的系统中检查DNS值

如果命令出错,错误应存储在不同的变量中

这就是我到目前为止所做的:

proc = subprocess.Popen(['echo', '"to stdout"'], stdout=subprocess.PIPE,)

stdout_value = proc.communicate()
print '\tstdout:', repr(stdout_value)

subprocess.call('echo #user', shell=True)
#subprocess.check_call('echo #HOME', shell=True)

Tags: to命令echotruevaluedns系统错误
1条回答
网友
1楼 · 发布于 2024-04-25 00:12:54

你应该试试这个:

它从作为参数传递的命令中捕获errorcode、stdout和stderr: 导入shlex 从子流程导入Popen、PIPE

def get_exitcode_stdout_stderr(cmd):

    """
    Execute the external command and get its exitcode, stdout and stderr.
    """
    args = shlex.split(cmd)

    proc = Popen(args, stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    exitcode = proc.returncode
    #
    return exitcode, out, err

cmd = "..."  # arbitrary external command, e.g. "python mytest.py"
exitcode, out, err = get_exitcode_stdout_stderr(cmd)

根据您的需要,我认为您可以使用python模块来获得所需的内容,而不是使用bash cmd行。例如,要获得完全限定的域名,您可以使用:

socket.getfqdn()

相关问题 更多 >