1

I am trying to test a specific server is up and running on a certain port so I am using $result = `echo exit | telnet 127.0.0.1 9443`; print $result;

Here I am using localhost for privacy issues The expected behavior is that it should print "...Could not open connection to the host, on port 9443: Connect failed", this way I know that the server is not running. but it prints an empty string

Any help on this

zdim
  • 64,580
  • 5
  • 52
  • 81

1 Answers1

3

The failure message is printed to STDERR, while backticks return only what goes to STDOUT.

You can redirect the STDERR stream to the STDOUT stream

$result = `echo exit | telnet 127.0.0.1 9443 2>&1`; 

See I/O redirection.


There are more rounded ways to do this, using various forms of open. See it in perlfaq8. There are also various modules for this. The Capture::Tiny makes it rather easy.

use warnings 'all';
use strict;

use Capture::Tiny qw(capture);

my $cmd = 'echo exit | telnet 127.0.0.1 9443';

my ($stdout, $stderr) = capture {
  system ( $cmd );
};

print "STDOUT: $stdout";
print "STDERR: $stderr";

This prints for me

STDOUT: Trying 127.0.0.1...
STDERR: telnet: connect to address 127.0.0.1: Connection refused

The module has many more capabilities. From the docs

Capture::Tiny provides a simple, portable way to capture almost anything sent to STDOUT or STDERR, regardless of whether it comes from Perl, from XS code or from an external program.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • It still prints an empty string I've tried it. When I try other commands like `which java` it prints the output correctly, but in telnet command it doesn't – Ahmed Abuzekry Oct 17 '16 at 07:53
  • @AhmedAbuzekry That seems strange. I tried it and I get back the message. – zdim Oct 17 '16 at 07:54
  • I don't know I tried it before and it is not working – Ahmed Abuzekry Oct 17 '16 at 07:57
  • @AhmedAbuzekry Did you try to run `echo exit | telnet ...` on the command line? Also, where do you do all this, what system/environment? – zdim Oct 17 '16 at 07:58
  • Yes I've tried to run it on the command line and it prints the message normally. I am using windows and trying to run this from a perl script to check for the environment before running a test. – Ahmed Abuzekry Oct 17 '16 at 08:02
  • @AhmedAbuzekry Heh .. it should work on Windows as it stands, but it may depend on what you use for Perl. I can't test on Windows now :( Another -- better -- option is to use a more solid tool for capturing. For one thing, there is a well-regarded module [Capture::Tiny](http://search.cpan.org/~dagolden/Capture-Tiny-0.44/lib/Capture/Tiny.pm). It is very simple to use. – zdim Oct 17 '16 at 08:24
  • @AhmedAbuzekry I added a few links to the code, and sample code for `Capture::Tiny` – zdim Oct 17 '16 at 08:34
  • Thank you very much for the help – Ahmed Abuzekry Oct 17 '16 at 10:21
  • Another possibility _might_ be `use File::Spec; my $devnull = File::Spec->devnull(); my $rc = system("echo exit | telnet 127.0.0.1 9443 1>$devnull 2>$devnull");` and then check the `$rc`. But I cannot try this out right now. – PerlDuck Oct 17 '16 at 10:26