How to get result of openssl command in .txt file

1

Someone knows how to get the result of the following command: openssl x509 -noout -modulus -in file.pem in a .txt file. The command works well when I execute it both in the console and in the php exec function, which is not how to get its result in a .txt file. Thank you in advance, I hope you can help me.

    
asked by Antonio Perez 20.04.2016 в 19:16
source

4 answers

3

If you are in Windows you can simply get it in the console by redirecting the standard output to your file in the following manner

C:\> openssl x509 -noout -modulus -in file.pem > archivo.txt
    
answered by 20.04.2016 / 19:43
source
1

The PHP documentation specifies that the exec() function can have from one to three parameters:

string exec ( string $command [, array &$output [, int &$return_var ]] )

Surely now you are calling it with just one (the command), but you would be interested in passing it a second parameter (output). This second parameter returns an array with the command output (line by line).

From the documentation linked above (my translation):

  

output

     

If the output argument is present, then the specified array will be filled with each line of the command output. The blanks at the end, as well as \ n, are not included in this array. Note that if the array contains elements, exec () will add to the end of the array. If you do not want the function to add elements to the array, call unset () in the array before passing it to exec ().

Then, you only have to go through the second parameter to see the result of the operation performed with exec() and save it in a file.

If you want to do it without PHP, and you can add something at the end of the command, you could do something like this:

$miComando .= " > fichero.txt";

This will cause the string "> file.txt" to be added to the end of your command, which in cmd and shell will write the result in a txt file for you.

    
answered by 20.04.2016 в 19:38
0

There is also the option to use the command line execution controls:

If you have given return code 0 what will happen next to & & amp;

C:\>openssl&&@echo ha ido bien

or

If we want to control when there is an error, we will double-pipe and then execute what follows the ||

C:\>openssl||@echo ha dado error

    
answered by 08.05.2016 в 22:44
0

If the command returns a return code, it is stored in the variable errorlevel :

C:\>openssl

"openssl" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.'

C:\>@echo %errorlevel%
9009
    
answered by 08.05.2016 в 22:35