Python 如何在此处拆分命令以使用subprocess.Popen()

Python 如何在此处拆分命令以使用subprocess.Popen(),python,command,subprocess,Python,Command,Subprocess,我不确定在哪里放置逗号来分隔它们,以便使用subprocess.Popen使用此命令。有人知道吗?以下是我的建议 创建包含此内容的文件-将其称为“路由信息”,并使其可执行: ip = subprocess.Popen(["/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') | grep \"inet addr\" | awk -F: '{print $2}' | awk \'{print $1}\'"], stdout=subpr

我不确定在哪里放置逗号来分隔它们,以便使用subprocess.Popen使用此命令。有人知道吗?

以下是我的建议

创建包含此内容的文件-将其称为“路由信息”,并使其可执行:

ip = subprocess.Popen(["/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') | grep \"inet addr\" | awk -F: '{print $2}' | awk \'{print $1}\'"], stdout=subprocess.PIPE)
在python程序中,使用:

#!/bin/sh

/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') |
    grep "inet addr" |
    awk -F: '{print $2}' |
    awk '{print $1}'
这样,您就不必担心引用字符,您可以独立测试
路由信息
脚本,以确保其正常工作

脚本
route info
不接受任何命令行参数,但如果它接受,您将通过以下方式传递它们:

ip = subprocess.Popen(["/path/to/route-info"], stdout=subprocess.PIPE)

以下是我的建议

创建包含此内容的文件-将其称为“路由信息”,并使其可执行:

ip = subprocess.Popen(["/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') | grep \"inet addr\" | awk -F: '{print $2}' | awk \'{print $1}\'"], stdout=subprocess.PIPE)
在python程序中,使用:

#!/bin/sh

/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') |
    grep "inet addr" |
    awk -F: '{print $2}' |
    awk '{print $1}'
这样,您就不必担心引用字符,您可以独立测试
路由信息
脚本,以确保其正常工作

脚本
route info
不接受任何命令行参数,但如果它接受,您将通过以下方式传递它们:

ip = subprocess.Popen(["/path/to/route-info"], stdout=subprocess.PIPE)

以下是我的建议

创建包含此内容的文件-将其称为“路由信息”,并使其可执行:

ip = subprocess.Popen(["/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') | grep \"inet addr\" | awk -F: '{print $2}' | awk \'{print $1}\'"], stdout=subprocess.PIPE)
在python程序中,使用:

#!/bin/sh

/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') |
    grep "inet addr" |
    awk -F: '{print $2}' |
    awk '{print $1}'
这样,您就不必担心引用字符,您可以独立测试
路由信息
脚本,以确保其正常工作

脚本
route info
不接受任何命令行参数,但如果它接受,您将通过以下方式传递它们:

ip = subprocess.Popen(["/path/to/route-info"], stdout=subprocess.PIPE)

以下是我的建议

创建包含此内容的文件-将其称为“路由信息”,并使其可执行:

ip = subprocess.Popen(["/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') | grep \"inet addr\" | awk -F: '{print $2}' | awk \'{print $1}\'"], stdout=subprocess.PIPE)
在python程序中,使用:

#!/bin/sh

/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') |
    grep "inet addr" |
    awk -F: '{print $2}' |
    awk '{print $1}'
这样,您就不必担心引用字符,您可以独立测试
路由信息
脚本,以确保其正常工作

脚本
route info
不接受任何命令行参数,但如果它接受,您将通过以下方式传递它们:

ip = subprocess.Popen(["/path/to/route-info"], stdout=subprocess.PIPE)

您正在使用shell功能(管道),因此不应拆分命令,而应使用
shell=True

ip = subprocess.Popen(["/path/to/route-info", arg1, arg2, ...], stdout=subprocess.PIPE)

您正在使用shell功能(管道),因此不应拆分命令,而应使用
shell=True

ip = subprocess.Popen(["/path/to/route-info", arg1, arg2, ...], stdout=subprocess.PIPE)

您正在使用shell功能(管道),因此不应拆分命令,而应使用
shell=True

ip = subprocess.Popen(["/path/to/route-info", arg1, arg2, ...], stdout=subprocess.PIPE)

您正在使用shell功能(管道),因此不应拆分命令,而应使用
shell=True

ip = subprocess.Popen(["/path/to/route-info", arg1, arg2, ...], stdout=subprocess.PIPE)


使用。具体如何使用?只需导入模块并执行
shlex.split('your long command')
Use。具体如何使用?只需导入模块并执行
shlex.split('your long command')
Use。具体如何使用?只需导入模块并执行
shlex.split('your long command'))
Use。具体如何使用?只需导入模块并执行
shlex.split('your long command')
就可以了!我得试试看!我得试试看!我得试试看!超级的!救了我一天。您还可以告诉我什么可以在Mac上使用,以及此路由信息如何将Mac与我将在CentOS上使用的上述路由区分开来?要在OSX上查找默认路由,请使用
route-n get default
。我认为您应该为每个操作系统创建特定的版本,并在安装脚本时安装正确的版本,即在安装时确定操作系统,而不是在运行时。超级!救了我一天。您还可以告诉我什么可以在Mac上使用,以及此路由信息如何将Mac与我将在CentOS上使用的上述路由区分开来?要在OSX上查找默认路由,请使用
route-n get default
。我认为您应该为每个操作系统创建特定的版本,并在安装脚本时安装正确的版本,即在安装时确定操作系统,而不是在运行时。超级!救了我一天。您还可以告诉我什么可以在Mac上使用,以及此路由信息如何将Mac与我将在CentOS上使用的上述路由区分开来?要在OSX上查找默认路由,请使用
route-n get default
。我认为您应该为每个操作系统创建特定的版本,并在安装脚本时安装正确的版本,即在安装时确定操作系统,而不是在运行时。超级!救了我一天。您还可以告诉我什么可以在Mac上使用,以及此路由信息如何将Mac与我将在CentOS上使用的上述路由区分开来?要在OSX上查找默认路由,请使用
route-n get default
。我认为您应该为每个操作系统创建特定版本,并在安装脚本时安装正确的版本,即在安装时确定操作系统,而不是在运行时确定操作系统。