2012-01-20 24 views
2

我在Perl中的兩臺Linux(centos)機器之間運行iperf。服務器端生成顯示在底部的輸出。我需要收集測試持續時間(本例中爲0.4秒)並將其放入一個變量中。這不應該很難,但是我很難過,因爲我是一個perl新手。如何在Perl中收集「iperf」輸出

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 

回答

1

您可以使用此正則表達式從命令輸出

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

提取「0.4秒」。例如:

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"; 
    } 
} 

注:該腳本將等待命令輸出,直到你用手中斷

+0

非常感謝,非常有幫助 – user1161409