How to use tee command inside child process (exec)?

血红的双手。 提交于 2019-12-24 18:23:31

问题


I'm trying to use the tee command inside the exec, but I can't get any progress. My command converts a PFX to a PEM certificate using openssl like that:

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

Terminal output:

# 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-----

The first part of the command above converts my PFX file to a PEM file (openssl pkcs12 -in MYCERT.pfx -nodes -passin pass:123456).

Soon after, the content from first is sent to the second command (openssl pkey -outform PEM) where I extract my private key and to the third command(openssl x509 -outform PEM) where I extract my public key.

It works perfectly at the Linux terminal, but fails when used by exec.

My Node code:

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 output:

 { 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)' }

I would like to run that command in this way because I will able to get the private and public key using a single exec command.

Can someone help-me?

Thanks a lot.

来源:https://stackoverflow.com/questions/56657542/how-to-use-tee-command-inside-child-process-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!