用同一台PC的两个网口实现Iperf的server端和client端

2023-07-12,,

用同一台PC的两个口实现Iperf的server端和client端

2015年10月20日 20:35:11

阅读数:2943

有时候需要发包,仅仅需要一定速率的流量,并不需要关心收到报文的大小,一个好用的开源软件发包工具并不好找,iperf发包很方便,但是一般需要两台电脑,分别作为server端和client端,如果使用一个PC的两个端口分别作为Iperf的Server端和Client端,只需要一台电脑,作为一个可携带的发包工具,会大大方便携带与使用。

将一台电脑的两个端口分别配置为不同的网段,如下:

     

    ifconfig eth2 10.50.0.1/24

     

    ifconfig eth3 10.50.1.1/24

通过NAT实现IP源IP和目的IP转换:

     

    # nat source IP 10.50.0.1 -> 10.60.0.1 when going to 10.60.1.1

     

    iptables -t nat -A POSTROUTING -s 10.50.0.1 -d 10.60.1.1 -j SNAT --to-source 10.60.0.1

     
     
     

    # nat inbound 10.60.0.1 -> 10.50.0.1

     

    iptables -t nat -A PREROUTING -d 10.60.0.1 -j DNAT --to-destination 10.50.0.1

     
     
     

    # nat source IP 10.50.1.1 -> 10.60.1.1 when going to 10.60.0.1

     

    iptables -t nat -A POSTROUTING -s 10.50.1.1 -d 10.60.0.1 -j SNAT --to-source 10.60.1.1

     
     
     

    # nat inbound 10.60.1.1 -> 10.50.1.1

     

    iptables -t nat -A PREROUTING -d 10.60.1.1 -j DNAT --to-destination 10.50.1.1

配置路由和ARP

     

    ip route add 10.60.1.1 dev eth2

     

    arp -i eth2 -s 10.60.1.1 00:1B:21:C1:F6:0F # eth3's mac address

     
     
     

    ip route add 10.60.0.1 dev eth3

     

    arp -i eth3 -s 10.60.0.1 00:1B:21:C1:F6:0E # eth2's mac address

检查网络

ping 10.60.1.1

运行iperf

     

    # server

     

    ./iperf -B 10.50.1.1 -s

     
     
     

    # client: your destination is the other end's fake address

     

    ./iperf -B 10.50.0.1 -c 10.50.1.1 -t 60 -i 10
    (原来csdn上的连接有一定的问题,-c 10.60.1.1测试不通)

TCPDUMP检查

tcpdump -nn -i eth2 -c 500

用同一台PC的两个网口实现Iperf的server端和client端的相关教程结束。

《用同一台PC的两个网口实现Iperf的server端和client端.doc》

下载本文的Word格式文档,以方便收藏与打印。