-3

I am trying to capture the certificate information of a particular site and send it to a file in windows using perl. To achieve this, I wrote the below code in certextract.pl:

$commandToExecute = "openssl s_client -connect varcert.test.com:8334";
my $filename = 'C:\F6\certoutput.txt';
my @cmdout = `$commandToExecute`;
open(my $cmd, '>', $filename) or die "Could not open file '$filename' $!";
print $cmd @cmdout;

But when I execute perl certextract.pl, this doesn't write the output to a file unless I hit enter as openssl command itself is not exiting unless I hit enter. i.e., the command openssl s_client -connect varcert.test.com:8334 doesn't exit unless I hit enter in the command prompt. Hence perl also is not exiting. As I am using this perl file in automation, I want the output to be written without manual intervention. Can someone please help me with this?

2
  • Try changing @cmdout to a scalar instead, $cmdout. That should concatenate the lines of your output instead. Your problem sounds like a terminal issue. Are you sure nothing is written to the file? Commented Jun 2 at 11:17
  • Also, is that the exact code that you are executing? Nothing else in your code? And you are executing it with perl certextract.pl in cmd window? Commented Jun 2 at 11:19

1 Answer 1

1

openssl s_client is connecting to the server and then sending data supplied by stdin to the server while sending data from the server (and debug information) to stdout. It will exit when the server has closed the connection. A HTTP server (which you might attempt to connect to) will typically only exit once it has read the request and send the response, or if an invalid request has been received or if the client has closed the connection. When none of this is happening (like the client connects but sends nothing) then it might close the connection after some timeout.

my @cmdout = `$commandToExecute`;

This statement will only be finished once the command has been finished, i.e. openssl s_client did exit. As long as you don't provide any input the server at the other end of the connection will probably wait and not exit. If you hit enter this will probably be seen as invalid input and the server will close the connection. Only then the command will be finished and this statement will be done.

One way to deal with this situation is to make sure that openssl s_client closes the connection to the server, so that the server will close the connection too and the command will be done. One a UNIX system this could be achieved with:

$commandToExecute = "openssl s_client -connect varcert.test.com:8334 < /dev/null";

Alternatives would be to use IPC::Open2 and close the file handle created for stdin.

Sign up to request clarification or add additional context in comments.

1 Comment

IPC::Run, though not core, is simpler to use than IPC::Open2: run [ "openssl", "sclient", "-connect", "varcert.test.com:8334" ], "<", "/dev/null", ">", \my $output;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.