如何在.ex文件中执行此openssl命令?

时间:2017-09-13 10:43:21

标签: openssl elixir

我试图在.ex文件中执行此命令 -

openssl ec -in myprivatekey.pem -outform DER|tail -c +8|head -c 32|xxd -p -c 32

,我已翻译成

  {_, 0} = System.cmd "openssl", [ "ec", "-in", private_key_file, "-outform", "DER|tail", "-c", "+8|head", "-c", "32|xxd", "-p", "-c", "32"], [stderr_to_stdout: true]
<\ n>在elixir中,但我得到以下错误 - error

如何正确执行此openssl命令?

1 个答案:

答案 0 :(得分:2)

您的第一个代码段实际上是执行多个命令(openssl,tail,head,xxd)和管道数据从一个到另一个。 System.cmd只生成一个命令,不会自动处理管道。

您可以使用:os.cmd/1来执行此操作,这将使用系统的默认shell生成命令,该shell应该处理管道:

# Note that this takes the command as a charlist and does not return the exit code
output = :os.cmd('openssl ec -in myprivatekey.pem -outform DER|tail -c +8|head -c 32|xxd -p -c 32')

另一种方法是使用System.cmd自己将命令传递给shell。以下内容适用于/bin/sh存在的系统:

{stdout, 0} = System.cmd("/bin/sh", ["-c", "openssl ec -in myprivatekey.pem -outform DER|tail -c +8|head -c 32|xxd -p -c 32"])
相关问题