Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

net::OpenSSH several commands, stdout are mixed up

by Bolemo (Acolyte)
on Sep 16, 2020 at 17:12 UTC ( [id://11121844]=perlquestion: print w/replies, xml ) Need Help??

Bolemo has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am quite new to Perl, and I am using smokeping.

I created a RemoteTraceroutePing Probe based on the RemoteTraceroute one. Basically, the idea is to create a SSH connection to a remote host, and fire regularly traceroute commands and read the output. So far, I was able to have something that works,, and it works with one or two probes, but with more than that, all standard outputs are mixed up.


I do create the connection this way:
# $ssh_sr contains the remote host like user@host open my $def_in, '<', '/dev/null'; $self->{ssh} = Net::OpenSSH->new($ssh_sr, default_stdin_fh => $def_in) +;
Then, the different targets (Traceroute commands) are called like this (in parallel):
# @cmd contains the traceping command to launch remotely via the exist +ing SSH connexion. my $killed; my (undef, $f_stdout, $f_stderr, $pid) = $self->{ssh}->open3(@cmd); while (<$f_stdout>){ # OUTPUT ANALYZED AND PROCESSED HERE $killed = kill(15, $pid); last; } waitpid $pid, 0; close $f_stdout; close $f_stderr;
The problem is, despite that I can launch several traceping simultaneously with individual pids, the f_stdout of one pid is receiving the stdout from others. How can I fix that ? Thank you

Replies are listed 'Best First'.
Re: net::OpenSSH several commands, stdout are mixed up
by salva (Canon) on Sep 17, 2020 at 06:29 UTC
    Then, the different targets (Traceroute commands) are called like this (in parallel)

    Unless you show us your real code including all the related pieces, it would be quite difficult for us to give you any proper advice.

      I tried something. I added a tee to the remote command to log its output in a file (on the host, a different file for each call).
      It appears that it is traceroute that is mixing up its outputs on the host; Net::OpenSSH is working fine.

      For info, what the recorded file through tee is returning (second line is wrong and belongs to another traceroute call, but the hop number is wrong as well as in the other call, it is hop #1, this ip is never hop #5).
      root@HERMES:/tmp/mnt/sda1$ cat test_77.95.71.196.tee traceroute to 77.95.64.123 (77.95.64.123), 6 hops max, 38 byte packets 5 10.2.0.211 8.592 ms 2.467 ms 2.718 ms 2.530 ms 2.561 ms 2.56 +1 ms 2.687 ms 2.749 ms 2.405 ms 2.561 ms 2.562 ms 2.562 ms 6 77.95.70.196 18.432 ms 18.244 ms 18.431 ms 18.119 ms 18.463 m +s 18.213 ms 18.619 ms 18.151 ms 18.369 ms 18.119 ms 18.556 ms +18.682 ms
      What the traceroute should return:
      root@HERMES:/tmp/mnt/sda1$ traceroute -w 1 -f 5 -m 6 -q 12 -n 77.95.64 +.123 traceroute to 77.95.64.123 (77.95.64.123), 6 hops max, 38 byte packets 5 77.95.71.196 18.806 ms 18.244 ms 18.619 ms 17.995 ms 18.807 m +s 18.463 ms 18.495 ms 18.307 ms 18.744 ms 18.276 ms 18.588 ms +18.307 ms 6 77.95.70.196 18.588 ms 18.276 ms 18.432 ms 18.089 ms 18.588 m +s 18.088 ms 18.463 ms 18.119 ms 18.432 ms 18.151 ms 18.588 ms +18.181 ms

      So this traceroute does not work properly when called multiple times at the same time.
      Net::OpenSSH output is similar to the one on the tee file, so behaves as expected.

        Well, maybe the ICMP packets coming back with the expired message notices are being captured by the wrong process.

        Try running a different command on the remote host instead of traceroute.

Re: net::OpenSSH several commands, stdout are mixed up
by jcb (Parson) on Sep 17, 2020 at 01:27 UTC

    That is because all of the $f_stdout values derived from the same SSH connection are the same handle. You will need a separate SSH channel for each individual traceping. Look at the "connection multiplexing" features in OpenSSH for more.

    Edit: So Net::OpenSSH already implements multiplexing, so the problem is something else.

      That is because all of the $f_stdout values derived from the same SSH connection are the same handle

      No, they are not. Every open3 call opens a new channel and returns a different set of pipes attached to the remote process stdin, stdout and stderr streams.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11121844]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-18 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found