2

我想执行这个linux命令

"cat cflow_image.py | mailx -s "CFLOW Copy" foo@.foo.com"。我的要求是在 python 脚本中使用这个命令。我正在使用 subprocess 模块来实现这一点。

这是我的一段代码,

def send_mail(mailid): 
   # This is mail the testbed info to the user
   mailid = args.mailID 
   print "* INFO file will be sent to your mailid *"
   subprocess.call("cat info.txt | mailx -s \"DEVSETUP\" {0}").format(mailid)

以下是执行时的错误,

Traceback (most recent call last):
  File "dev-installer.py", line 243, in <module>
    send_mail(args.mailID)
  File "dev-installer.py", line 204, in send_mail
    subprocess.call("cat info.txt | mailx -s \"DEVSETUP\" {0}").format(mailid)
  File "/sw/packages/python/2.7.4/lib/python2.7/subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/sw/packages/python/2.7.4/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/sw/packages/python/2.7.4/lib/python2.7/subprocess.py", line 1308, in _execute_child
4

2 回答 2

3

subprocess.call执行一个可执行文件。您没有将可执行文件的路径作为参数传递,而是在传递shell命令行。

要执行shell命令,您必须通过传入shell=True参数列表明确声明要在 shell 中执行命令。请注意,使用shell=True用户提供的命令可能存在安全隐患。

另请注意,当您使用shell=True时(例如,当您没有指定它时),您应该传递一个字符串列表作为参数,代表已经解析的参数列表。例如:

subprocess.call('ls -l')

将尝试执行名为的命令ls -l,如果您在 , 中没有具有该名称的可执行文件,则可能会失败PATH,而:

subprocess.call(['ls', '-l'])

ls使用参数调用可执行文件-l

如果您不想手动编写此类列表,请使用shlex.split解析“命令行”:

subprocess.call(shlex.split('executable arg1 arg2 --option1 --option2 "string argument"'))

将导致调用:

subprocess.call(['executable', 'arg1', 'arg2', '--option1', '--option2', 'string argument'])

请注意如何正确处理引号(但是没有执行 shell 扩展!)

于 2014-06-12T13:45:04.873 回答
1

你正在传递一个 shell 命令,所以 Python 应该调用一个 shell 来解析和运行你的命令。但是你告诉 Python 调用一个名为 的命令cat info.txt | mailx -s "DEVSETUP" foo@.foo.com,即在命令搜索路径上应该有一个同名的可执行文件——但不存在这样的命令。

subprocess.call支持Popen构造函数的所有关键字参数。您可以传递关键字参数shell=True来指示您拥有的是一些 shell 代码,而不是可执行文件的名称。

subprocess.call("cat info.txt | mailx -s \"DEVSETUP\" {0}".format(mailid),
                shell=True)

但是,请注意 的值mailid将被插入到 shell 代码段中,如果它包含 shell 特殊字符,它将中断,例如 inbob@example.com (Bob Smith)Bob Smith <bob@example.com>。您应该安排引用mailid.

您发布的命令的另一个问题是,如果在读取时发生任何错误info.txt,将不会被检测到。您可以通过避免无用的使用来解决这个问题cat

subprocess.call("<info.txt mailx -s \"DEVSETUP\" {0}".format(mailid),
                shell=True)

鉴于命令很简单,您根本不需要调用 shell。如果没有 shell 参与,您无需担心引用。您可以轻松地让 Python 打开info.txt文件进行读取。

body_file = open("info.txt")
status = subprocess.call(["mailx", "-s", "DEVSETUP", mailid], stdin=body_file)
body_file.close()
if status != 0:
    … handle mailx failure …
于 2014-06-12T14:47:07.593 回答