从 Popen() 调用脚本时,脚本参数未正确解析
Script arguments not parsed correctly when script called from Popen()
先简单介绍一下设置和目标;我 Core installed and a session running that I wish to run a few commands on from a python script. Since there are only a few commands and they are fairly simple commands, figured I'd just use subprocess.Popen
to kick of coresendmsg 而不是尝试在我的脚本中实现核心控制平面。
命令示例(来自 CLI 的 运行)如下:
coresendmsg exec node=2 num=1001 cmd='ifconfig eth0 192.168.0.1'
我在 运行 从 python 执行此命令并让 coresendmsg
正确解析 cmd 参数时遇到了一些问题。如果 运行 如下所示,coresendmsg
将 eth0 和 192.168.0.1 视为单独的参数和扼流圈,这对我来说很有意义。
s=subprocess.Popen(['coresendmsg',
'exec',
'-s 47854',
'node=2',
'num=1001',
'cmd=ifconfig eth0 192.168.0.1'])
如果我随后将其更改为以下内容,coresendmsg
现在将 cmd 视为单个参数,但仍然会阻塞,因为当参数在 coresendmsg
中拆分时,现在包含单引号.这与命令行中使用相同引号的 coresendmsg
为 运行 时不同,如上例所示。
s=subprocess.Popen(['coresendmsg',
'exec',
'-s 47854',
'node=2',
'num=1001',
'cmd=\'ifconfig eth0 192.168.0.1\''])
我知道这一点是因为我进入并修改了 coresendmsg 以打印参数并检查了两种情况。当来自 CLI 的 运行 以上命令时,我看到:
['exec', 'node=2', 'num=1001', 'cmd=ifconfig eth0 192.168.0.1']
但是当 运行 来自 python 时,根据我引用的方式,我得到以下之一:
['exec', 'node=2', 'num=1001', 'cmd=ifconfig', 'eth0', '192.168.0.1']
['exec', 'node=2', 'num=1001', "cmd='ifconfig eth0 192.168.0.1'"]
['exec', 'node=2', 'num=1001', 'cmd="ifconfig eth0 192.168.0.1"']
我可以通过 bash 脚本完成这项工作,但我不想 运行 脚本,从脚本,从脚本。正在寻找可能解决此问题的任何建议。
为了澄清@HaiVu 所说的内容,您的第一个示例失败是因为 -s 47854
,而不是 cmd
参数。在您的示例中,传递了 "-s 47854"
的单个参数。根据程序中参数的解析方式,它很可能将 node=2
作为 -s
.
的值
由于您使用的是列表,因此空格不会分隔参数,因此:
s=subprocess.Popen(['coresendmsg',
'exec',
'-s', '47854',
'node=2',
'num=1001',
'cmd=ifconfig eth0 192.168.0.1'])
您对引用差异的实验是可以预料的。当您从 shell ("command line") 运行 时,shell 会去掉引号。 运行 from Python 如果你考虑的话也会去掉引号。如果您转义引号,它们将直接传递,在 python 和 bash 中都是如此。
如果您需要额外的 shell 行为,请将 shell=True
参数用于 Popen
。在这种情况下不需要,并且在大多数情况下可以避免。
先简单介绍一下设置和目标;我 Core installed and a session running that I wish to run a few commands on from a python script. Since there are only a few commands and they are fairly simple commands, figured I'd just use subprocess.Popen
to kick of coresendmsg 而不是尝试在我的脚本中实现核心控制平面。
命令示例(来自 CLI 的 运行)如下:
coresendmsg exec node=2 num=1001 cmd='ifconfig eth0 192.168.0.1'
我在 运行 从 python 执行此命令并让 coresendmsg
正确解析 cmd 参数时遇到了一些问题。如果 运行 如下所示,coresendmsg
将 eth0 和 192.168.0.1 视为单独的参数和扼流圈,这对我来说很有意义。
s=subprocess.Popen(['coresendmsg',
'exec',
'-s 47854',
'node=2',
'num=1001',
'cmd=ifconfig eth0 192.168.0.1'])
如果我随后将其更改为以下内容,coresendmsg
现在将 cmd 视为单个参数,但仍然会阻塞,因为当参数在 coresendmsg
中拆分时,现在包含单引号.这与命令行中使用相同引号的 coresendmsg
为 运行 时不同,如上例所示。
s=subprocess.Popen(['coresendmsg',
'exec',
'-s 47854',
'node=2',
'num=1001',
'cmd=\'ifconfig eth0 192.168.0.1\''])
我知道这一点是因为我进入并修改了 coresendmsg 以打印参数并检查了两种情况。当来自 CLI 的 运行 以上命令时,我看到:
['exec', 'node=2', 'num=1001', 'cmd=ifconfig eth0 192.168.0.1']
但是当 运行 来自 python 时,根据我引用的方式,我得到以下之一:
['exec', 'node=2', 'num=1001', 'cmd=ifconfig', 'eth0', '192.168.0.1']
['exec', 'node=2', 'num=1001', "cmd='ifconfig eth0 192.168.0.1'"]
['exec', 'node=2', 'num=1001', 'cmd="ifconfig eth0 192.168.0.1"']
我可以通过 bash 脚本完成这项工作,但我不想 运行 脚本,从脚本,从脚本。正在寻找可能解决此问题的任何建议。
为了澄清@HaiVu 所说的内容,您的第一个示例失败是因为 -s 47854
,而不是 cmd
参数。在您的示例中,传递了 "-s 47854"
的单个参数。根据程序中参数的解析方式,它很可能将 node=2
作为 -s
.
由于您使用的是列表,因此空格不会分隔参数,因此:
s=subprocess.Popen(['coresendmsg',
'exec',
'-s', '47854',
'node=2',
'num=1001',
'cmd=ifconfig eth0 192.168.0.1'])
您对引用差异的实验是可以预料的。当您从 shell ("command line") 运行 时,shell 会去掉引号。 运行 from Python 如果你考虑的话也会去掉引号。如果您转义引号,它们将直接传递,在 python 和 bash 中都是如此。
如果您需要额外的 shell 行为,请将 shell=True
参数用于 Popen
。在这种情况下不需要,并且在大多数情况下可以避免。