0

What I am doing:

I have the following backticks command that executes in a simple foreach loop and saves the command output to a variable and I then perform string matching actions on that variable:

$ciphertestoutput = `echo -n | openssl s_client -cipher $tlsCipher -connect $ipaddress:443 2>/dev/null`;

The problem:

However, when I run my script in the output I get an error message that I cannot seem to stop appearing. I am not bothered that the error happens, but I do not want the error showing in the middle of the pretty command line output I have made.

My output and the error:

EXP-DES-CBC-SHA CIPHER IS SUPPORTED on 192.168.1.22:443

EXP-EDH-DSS-DES-CBC-SHA CIPHER IS NOT SUPPORTED on 192.168.1.22.443

EXP-RC2-CBC-MD5 CIPHER IS NOT SUPPORTED on 192.168.1.22:443
connect: Connection refused          <--- the error I cant get rid of
connect:errno=111                    <--- the error I cant get rid of

EXP-RC4-MD5 CIPHER IS NOT SUPPORTED on 192.168.1.22:443

What I have tried:

I have tried and experimented with all manner of ways I know of to suppress error messages in the output, but nothing I try prevents this error from appearing. I have done many similar things in the past and have never come across this issue with backticks. Is there something obvious I am missing here?

halfer
  • 19,824
  • 17
  • 99
  • 186
yonetpkbji
  • 1,019
  • 2
  • 21
  • 35
  • 1
    Using `2>/dev/null` is the correct method. Are you sure it's not working? – Ben Grimm Feb 08 '15 at 21:37
  • Yes I'm definitely getting the error (exactly as shown above) and I thought 2>/dev/null was the correct way to do it, that's why it was confusing me as well. The error can only be coming from this backtick command as well as it is the only 'meaningful' thing the script is doing (ie theres no other connection orientated or backtick commands going on in the script). So i cant understand why 2>/dev/null does not catch the error. – yonetpkbji Feb 08 '15 at 21:48

3 Answers3

4

Try to reopen STDERR in your program like this:

open STDERR, '>/dev/null';    
# your command

Errors of your qx-command will not be shown. So you don't have to worry about how will you call your program.

P.S. Also you can save your STDERR if you need it:

open OLDERR, ">&", \*STDERR; # or die "$!";
open STDERR, ">/dev/null"; # or die "$!";
# your command
open STDERR, ">&OLDERR"; # restoring your stderr
close OLDERR;
# other code
red0ct
  • 4,840
  • 3
  • 17
  • 44
2

Consider using IPC::Run3:

use IPC::Run3;    # Exports run3() by default

run3 \@cmd, \$in, \$out, \$err;

It separates out stdin, stdout, and stderr into different references. At minimum, it should help you figure out what is going on.

Borodin
  • 126,100
  • 9
  • 70
  • 144
FunkyShu
  • 138
  • 5
-1

Are you sure these are errors and not output from OpenSSL (coming to STDOUT instead of STDERR)? If all your expected output starts with EXP like your example you could try adding a

| grep EXP

to test.

Makyen
  • 31,849
  • 12
  • 86
  • 121
mstang
  • 157
  • 2