如何在Mininet中通过Python脚本实现主机间100个TCP数据包发送?
Hey there! Since you're already up and running with your Mininet topology, sending 100 TCP packets between two hosts using Python is totally manageable. Let’s walk through a practical, step-by-step approach that integrates directly with Mininet’s API.
Core Approach
The key idea is to leverage Mininet’s ability to execute commands on its emulated hosts. We’ll:
- Spin up a simple TCP listener on the target host (h2)
- Run a TCP client on the source host (h1) that sends exactly 100 packets
- Use Python’s built-in
socketmodule for reliable TCP communication (no extra tools needed, though we’ll also mention alternatives like Scapy for more control)
Full Implementation Code
Here’s a complete script that works with a basic 2-host-1-switch topology (replace the topology section with your existing one if needed):
from mininet.net import Mininet from mininet.node import Controller, OVSKernelSwitch from mininet.cli import CLI from mininet.log import setLogLevel, info import time def tcp_packet_demo(): # Initialize Mininet network (customize this part to match your existing topology) net = Mininet(controller=Controller, switch=OVSKernelSwitch) info('*** Adding controller\n') net.addController('c0') info('*** Adding hosts\n') h1 = net.addHost('h1', ip='10.0.0.1/24') h2 = net.addHost('h2', ip='10.0.0.2/24') info('*** Adding switch\n') s1 = net.addSwitch('s1') info('*** Creating links\n') net.addLink(h1, s1) net.addLink(h2, s1) info('*** Starting network\n') net.start() # Step 1: Start TCP listener on h2 info('*** Launching TCP listener on h2\n') h2.cmd('python3 -c "import socket; \ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); \ s.bind((\'10.0.0.2\', 8080)); \ s.listen(1); \ conn, addr = s.accept(); \ print(\'✅ Connected to:\', addr); \ count = 0; \ while count < 100: \ data = conn.recv(1024).decode(); \ if data: \ print(f\'📥 Received: {data}\'); \ count +=1; \ conn.close(); \ print(f\'✅ Received all {count} packets!\')" &') # Give the listener a second to start up time.sleep(1) # Step 2: Send 100 TCP packets from h1 to h2 info('*** Sending 100 TCP packets from h1\n') h1.cmd('python3 -c "import socket, time; \ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); \ s.connect((\'10.0.0.2\', 8080)); \ # Disable Nagle\'s algorithm to ensure individual packets are sent s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1); \ for i in range(100): \ msg = f\'TCP Packet #{i+1}\'; \ s.send(msg.encode()); \ print(f\'📤 Sent: {msg}\'); \ time.sleep(0.01); # Small delay to avoid packet merging s.close(); \ print(\'✅ All packets sent!\')"') # Optional: Verify with tcpdump (uncomment to enable) # info('*** Capturing packets with tcpdump on h2\n') # h2.cmd('tcpdump -i h2-eth0 tcp port 8080 -c 100 -l &') info('*** Dropping into Mininet CLI for further testing\n') CLI(net) info('*** Shutting down network\n') net.stop() if __name__ == '__main__': setLogLevel('info') tcp_packet_demo()
Key Details to Note
- Topology Customization: If you already have a pre-built topology, replace the network initialization section (from
net = Mininet(...)tonet.start()) with your existing code. Just make sure you have references to your source (h1) and target (h2) hosts. - TCP_NODELAY: We disable Nagle’s algorithm to prevent the OS from merging small outgoing packets into a single TCP segment—this ensures each
send()call translates to a distinct TCP packet. - Verification: The listener on h2 will print each received packet and confirm once all 100 are received. You can also uncomment the
tcpdumpline to capture and inspect the packets directly.
Alternative: Using Scapy for Raw TCP Packets
If you want more low-level control (e.g., customizing TCP flags), you can use Scapy. First, install Scapy on your Mininet host, then modify the send command on h1:
h1.cmd('scapy -c "from scapy.all import IP, TCP, send; \ for i in range(100): \ send(IP(dst=\'10.0.0.2\')/TCP(dport=8080)/f\'Raw Packet #{i+1}\', verbose=1)"')
Note: Raw TCP packets sent via Scapy won’t establish a full TCP connection (no three-way handshake), so the listener code above won’t pick them up. For this method, use tcpdump to verify packet delivery instead.
内容的提问来源于stack exchange,提问作者Babak Memar

