我创建了一个运行iperf流量的函数。代码按预期工作。该函数可以支持TCP和UDP协议。如果协议不作为参数传递,则默认使用tcp。对于UDP,-u作为参数传递。我已经使用unless
语句进行此检查。我被要求使用hashmap而不是except语句。有人可以告诉我如何在这种情况下使用哈希映射吗?
sub start_iperf_on_client_forward_traffic {
my ($self,$cli_interface_ip,$origin_interface_ip, $port,$protocol) = @_;
unless (defined $protocol)
{
$protocol = "";
}
# start iperf3 on client. If protocol is not specified tcp protocol will be used..
$self->{ssh}->execute( 'iperf3 ' . $protocol . ' -c ' . $origin_interface_ip . ' -B ' .
$cli_interface_ip . ' -i ' . 2 . ' -p ' . $port . ' -l ' . 576.
' -b ' . 100 . 'M ' . ' -k ' . 1000 );
my @stdout = $self->{ssh}->stdOut();
print Dumper @stdout;
}
函数调用:
$self->{'traffic_obj'} = Iperf->new(ip => "198.18.193.151" )
$self->{'client'} = $self->{'traffic_obj_1'}->start_iperf_on_client_forward_traffic('9.1.1.2','8.2.0.2','7755','-u');
输出:
$VAR1 = 'Connecting to host 8.2.0.2, port 7755';
$VAR2 = '[ 4] local 9.1.1.2 port 47105 connected to 8.2.0.2 port 7755';
$VAR3 = '[ ID] Interval Transfer Bandwidth Total Datagrams';
$VAR4 = '[ 4] 0.00-2.31 sec 5.49 MBytes 20.0 Mbits/sec 10000 ';
$VAR5 = '- - - - - - - - - - - - - - - - - - - - - - - - -';
$VAR6 = '[ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams';
$VAR7 = '[ 4] 0.00-2.31 sec 5.49 MBytes 20.0 Mbits/sec 0.279 ms 3751/9987 (38%) ';
$VAR8 = '[ 4] Sent 9987 datagrams';
$VAR9 = '';
$VAR10 = 'iperf Done.';