user3819295 user3819295 - 9 months ago 47
Perl Question

Convert shell command to pure Perl

Please help me to find a way to convert this shell command to pure Perl

ssh $IP1 netstat -Aan | awk '{print $5" "$6}' | grep 2049 | egrep -v "$IP1|$IP2" | wc -l

I want to avoid starting a separate shell process just to execute this command.

This is how the output of the first part of the command should look

ssh $IP1 netstat -Aan | awk '{print $5" "$6}'

I tried several ways and failed

@found = grep { /2049/ && !/$IP/ } `$SSH $FacingIP[0] netstat -Aan`;

I just need to add the

Answer Source

If I understand your question correctly, it could be better written as follows:

How does one extract the fifth and sixth whitespace-separated field from a list of lines.

For example,

my @lines = (
    "a b c d\n",
    "a b c d\n",
    "a b c d\n",

should produce

[ '', '' ],
[ '', '' ],
[ '', '' ],

I'd use

map { [ (split)[4,5] ] } @lines
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download