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

Hello, back again. I figured out how to read a remote log file with Control::CLI with a pipe so I can process the lines of the log. However, my debug line just inside the while never executes, leading me to believe the while never starts. Here's my code:

open (PRM, "-|", $NE_obj->cmd("ssh $activepilotrcs reboot ; tailer PRM +")); while (<PRM>) { print "DEBUG: $_\n"; if ($_ =~ m/.*FAILED HEARTBEAT.*/sxi) { print "$spacer $spacer $spacer $spacer Alarm Received That Act +ive Pilot Has Gone Down...\n"; } elsif ($_ =~ m/.*STARTING OA&M LEAD TRANSITION.*/sxi) { print "$spacer $spacer $spacer $spacer Standby Pilot $stdbypil +otrcs Has Started Transitioning To Active...\n"; } elsif ($_ =~ m/.*FAILOVER INITIALIZATION COMPLETED.*/sxi) { print "$spacer $spacer $spacer $spacer Standby Pilot $stdbypil +otrsc Has Transitioned To Active...\n"; $NE_output = $NE_obj->cmd("\003"); last; } else { next; } }

Am I missing something basic here? Why does my debug print line never actually print? Any thoughts on how I can get this working properly? Thanks in advance!

Replies are listed 'Best First'.
Re: Read Pipe, Never Enters While Loop
by Corion (Patriarch) on Jun 02, 2017 at 17:59 UTC
    open (PRM, "-|", $NE_obj->cmd("ssh $activepilotrcs reboot ; tailer PRM +"));

    Where in the documentation of Control::CLI did you find that usage?

    To me it seems that ->cmd returns a string of the command output, not a filehandle to be read from. That's also what the documentation says:

    Sends a CLI command to host and returns output data

    Maybe consider checking whether your open call succeeds at all:

    open (PRM, "-|", $NE_obj->cmd("ssh $activepilotrcs reboot ; tailer PRM +")) or die "Couldn't open filehandle : $!";

    But if you're using ssh anyway, why not run the commands directly through it?

    my $output = `(ssh $activepilotrcs reboot ; tailer PRM) |`;

    Or, instead, use Net::SSH::Any and use that to send the SSH commands yourself.

      Where in the documentation of Control::CLI did you find that usage?

      That is the syntax for sending a command to the remotely connected device. That part works fine...

      Maybe consider checking whether your open call succeeds at all

      I can see my open works in the logs created by Control::CLI. I can see in the input and debug logs the output of the tailer command.

      But if you're using ssh anyway, why not run the commands directly through it?

      In earlier code (not shown), I am connected to another blade on the machine already. The command in my open call is so I can SSH from the already established connection to another blade on the server. Straight SSH won't work in this case...

      Thank you Corjon for the feedback - I truly appreciate it!!

        What fails is your call to open.

        If you don't check that, you will wonder forever why Perl does not seem to read from that file.

        What works is your call to ->cmd, but that doesn't return a filehandle but the output of the command you run on the remote end.