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

Hello Guys,
Tried to to netstat on remote computer within perl/cgi script, but no luck as the output is gobbledygook and I dont know how perl read pipe symbol |, any other ideas guys?

currently my code is (working without pipe - gobbledygook output)
my @out;
@out = `psexec \\\\Server_Name -u Domain\\username -p Password netstat -na`;
print @out;


my code is (not working with pipe)
my @out;
@out = `psexec \\\\Server_Name -u Domain\\username -p Password netstat -na | find "port_number" | findstr "ESTABLISHED"`;
print @out;

Please advise

Thanks in advance
Win
  • Comment on netstat on remote with perl (possible?)

Replies are listed 'Best First'.
Re: netstat on remote with perl (possible?)
by McA (Priest) on Oct 24, 2013 at 14:09 UTC

    Hi,

    I don't know how the output looks like, but you can do the searching in the result also in perl itself. So have a look at your output with:

    print Dumper(\@out);

    You have to put a use Data::Dumper; at the top of your script.

    Searching in the array of output lines can be done this way:

    foreach my $line (@out) { next unless $line =~ /port_number/; # skip unrelevant lines print $line; # extract or search for ESTABLISHED }

    Best regards
    McA

      Thanks McA for your comment, the output is not a problem anymore but the pipe to execute the netstat is still not working.. any ideas or do you have any other solution? Cheers, Win

        Hi,

        Probably I'm totally wrong, but I thought that you search for a certain information in the output of your netstat, isn't it?

        I don't have an environment to test on, so I just assumed that the output of the netstat command is piped to find and then to findstr.

        My proposal was: If piping doesn't work you can grep the information on the perl side and not on the server side.

        Sorry, when I was totally wrong with my assumption. Let us see an example output of the netstat and what you want to search for.

        Regards
        McA

Re: netstat on remote with perl (possible?)
by kcott (Archbishop) on Oct 24, 2013 at 16:08 UTC

    G'day Win,

    Welcome to the monastery.

    While the output may appear to be "gobbledygook", it could provide useful troubleshooting tips. You need to help us to help you by providing this sort of information.

    Have you tried these commands at a shell prompt? If you have, what was the outcome? If not, please do so.

    The use of find in | find "port_number" | doesn't look right to me. In the absence of any options, the first argument to find is a pathname. Is that command correct as written? Did you perhaps mean something else, e.g. grep?

    -- Ken

      Nice catch.  FINDused to be restricted to working on files, whereas FINDSTRwas designed to be very grep-like.

      Those same rules might still apply; I haven't used FINDin a long, long time.

Re: netstat on remote with perl (possible?)
by marinersk (Priest) on Oct 24, 2013 at 16:18 UTC
    Interesting. I haven't used psexecin awhile, but it's coming back to me as I play with this.

    The problem is that psexeccan only run a program. The following summarizes my test:

    FAILURE: psexec dir C:\RmtTools SUCCESS: psexec cmd /c dir C:\RmtTools FAILURE: psexec cmd /c dir C:\RmtTools | sort /r

    1. dirfails because it is not a program (not a transient command). It is a resident command, which is not a program.
    2. cmd /c dirworks because cmdis a transient command -- an executable program in its own right. It happens to process the command line with the /coption, so basic command line rules apply and dirtherefore works.
    3. One of two things is failing with the use of a pipe in this third example; either psexecis filtering it out before sending it, or it is not being handled properly on the other end by the cmdprogram.

    The full test is hidden behind the readmore tag below.

    HOW I SOLVED THIS IN THE PAST: My suggestion is to copy a batch file to the remote system which has your commands in it, and which redirects its final output to a file; then use psexecto execute that batch file; then copy the results file back and parse it.

    If you can, it is handy to create a Unique but Known directory on the remote system where such things can be deposited.

    There are other ways to work around this limitation -- but psexeccan only run one program -- it does not, in the strictest sense, execute a command line. Pipes are not part of that operating condition.

Re: netstat on remote with perl (possible?)
by marinersk (Priest) on Oct 24, 2013 at 15:47 UTC
    Odd, I have Cygwin installed, permitting access to lsand grepcommands. This works fine on my system:

    #!/usr/bin/perl use strict; { my @ret1 = `ls`; print "[@ret1]\n"; my @ret2 = `ls | sort -r`; print "[@ret2]\n"; } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-10-24@0940-BackTick-Pipe>testbacktick.pl [test1.dat test2.dat test3.dat testbacktick.pl ] [testbacktick.pl test3.dat test2.dat test1.dat ]
    Added: Could it be that psexecmight be messing with your command string? Maybe you can pre-escape the pipes with backslashes or somesuch?