user1161409
user1161409

Reputation: 21

How to Collect "iperf" output in Perl

I'm running iperf between two linux (centos) machines in Perl. The server side generates the output shown down the bottom. I need to collect the test duration (0.4 sec in this case) and put it in a variable. It shouldn't be hard but I'm having a hard time since I'm a perl newbie.

Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
[  4] local 192.168.18.2 port 5001 connected with 192.168.17.2 port 32840
[  4]  0.0- 0.4 sec  10240 KBytes  210890 Kbits/sec

Upvotes: 2

Views: 1438

Answers (1)

Taras
Taras

Reputation: 920

You can use this regexp to extract "0.4 sec" from command output

/^\[\s*\d+\]\s*\d+\.\d+\s*\-\s*(\d+\.\d+)\s*sec/

For example:

use IPC::Open2;

my $pid = open2(\*CHLD_OUT, \*CHLD_IN, "iperf -s"); # open iperf server process

my $time_duration;

while (<CHLD_OUT>) { # waiting for command output
  if (/^\[\s*\d+\]\s*\d+\.\d+\s*\-\s*(\d+\.\d+)\s*sec/) {
    $time_duration = $1;
    print "time duration: $1\n";
  }
}

note: this script will be waiting for command output until you interrupt it by hand

Upvotes: 1

Related Questions