如何在子进程(exec)中使用tee命令?

时间:2019-06-18 21:55:02

标签: node.js openssl child-process pem pfx

我正在尝试在exec中使用tee命令,但无法获得任何进展。 我的命令使用如下的openssl将PFX转换为PEM证书:

openssl pkcs12 -in MYCERT.pfx -nodes -passin pass:123456 | tee >(openssl pkey -outform PEM) >(openssl x509 -outform PEM)

端子输出:

# first command output (PFX to PEM)
Bag Attributes
    localKeyID: 1A 0F 62 99 24 06 E7 AE D7 0F 11...

-----BEGIN CERTIFICATE-----
MIIH+zCCBeOgAwIBAgIQXSw4HKLTQrztT3ENqlcWfjANBgkqhkiG9w0BAQsFADB4
MQswCQYDVQQGEwJCUjETMB ...
-----END CERTIFICATE-----

# second command output (public key)
-----BEGIN CERTIFICATE-----
MIIH+zCCBeOgAwIBAgIQXSw4HKLTQrztT3ENqlcWfjANBgkqhkiG9w0BAQsFADB4
MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXN...
-----END CERTIFICATE-----

# third command output (private key)
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDES17GiaEXojOa
EoK5cf5mJp+saKXst3uqbznnf...
-----END PRIVATE KEY-----

上面命令的第一部分将我的PFX文件转换为PEM文件(openssl pkcs12 -in MYCERT.pfx -nodes -passin pass:123456)。

此后不久,第一者的内容被发送到第二个命令(openssl pkey -outform PEM),在其中我提取我的私钥,并发送到第三命令(openssl x509 -outform PEM),在其中我提取我的公钥。 >

它在Linux终端上完美运行,但是在exec使用时失败。

我的节点代码:

const fs = require('fs');
const path = require('path');
const uuid = require('uuid/v4');
const { exec } = require('child_process');

exports.readPkcs12 = (pfx, password, callback) => {
  const fileName = path.join('/', 'tmp', `${uuid()}.pfx`);
  fs.writeFileSync(fileName, pfx);

  exec(
    `openssl pkcs12 -in ${fileName} -nodes -passin pass:${password} | tee >(openssl x509 -outform PEM) >(openssl pkey -outform PEM)`,
    (error, stdout, _stderr) => {
      console.log(stdout, error);
      if (error) return callback(error);
      callback(null, stdout);
    }
  );
};

错误输出:

 { Error: Command failed: openssl pkcs12 -in /tmp/5dcee6b8-6b25-4765-bd17-c0a84d2bcbb5.pfx -nodes -passin pass:brlog18 | tee >(openssl x509 -outform PEM) >(openssl pkey -outform PEM)
/bin/sh: 1: Syntax error: "(" unexpected

    at ChildProcess.exithandler (child_process.js:299:12)
    at ChildProcess.emit (events.js:193:13)
    at ChildProcess.EventEmitter.emit (domain.js:481:20)
    at maybeClose (internal/child_process.js:999:16)
    at Socket.stream.socket.on (internal/child_process.js:403:11)
    at Socket.emit (events.js:193:13)
    at Socket.EventEmitter.emit (domain.js:481:20)
    at Pipe._handle.close (net.js:614:12)
  killed: false,
  code: 2,
  signal: null,
  cmd:
   'openssl pkcs12 -in /tmp/5dcee6b8-6b25-4765-bd17-c0a84d2bcbb5.pfx -nodes -passin pass:brlog18 | tee >(openssl x509 -outform PEM) >(openssl pkey -outform PEM)' }

我想以这种方式运行该命令,因为我将能够使用一个exec命令来获取私钥和​​公钥。

有人可以帮我吗?

非常感谢。

0 个答案:

没有答案