Traffic Generator with Nping on Linux – Multiple UDP Streams

This example uses nping to generate multiple independent UDP traffic streams from a Linux server. Each stream uses random source and destination UDP ports and runs continuously until the script is stopped with Ctrl+C.

Install Nping

On Ubuntu, nping is included in the Nmap package:

sudo apt update
sudo apt install nmap -y

Verify that nping is available:

nping --version

Traffic Generator Script

Create the traffic generator script:

nano traffic_gen.sh

Example script:

#!/bin/bash
DST_IP="203.0.113.10"
STREAMS=10
PPS=509
PAYLOAD=1200
NPING_PIDS=()
cleanup() {
echo
echo "Stopping traffic..."
for pid in "${NPING_PIDS[@]}"; do
kill "$pid" 2>/dev/null
done
exit 0
}
trap cleanup INT TERM
for i in $(seq 1 $STREAMS); do
SPORT=$((1024 + RANDOM % 50000))
DPORT=$((1024 + RANDOM % 50000))
nping --udp "$DST_IP" \
-p "$DPORT" \
-g "$SPORT" \
--data-length "$PAYLOAD" \
--rate "$PPS" \
--count 0 \
--no-capture \
-H >/dev/null 2>&1 &
NPING_PIDS+=($!)
done
echo "Started $STREAMS UDP streams (~5 Mbps each) to $DST_IP"
echo "Press Ctrl+C to stop"
# wait forever until Ctrl+C
while true; do
sleep 1
done

Configure the Traffic

The main parameters are defined at the beginning of the script:

DST_IP="203.0.113.10"
STREAMS=10
PPS=509
PAYLOAD=1200

DST_IP is the destination IP address, STREAMS controls the number of independent UDP flows, PPS defines packets per second for each stream, and PAYLOAD defines the UDP payload size in bytes.

Replace the example destination address with the IP address of the system used for your authorized network test.

Traffic Rate

With a 1200-byte payload and 509 packets per second, each stream generates approximately 4.9 Mbps of payload traffic:

1200 bytes × 509 pps × 8 = 4.8864 Mbps

With 10 streams, the total payload traffic is approximately:

~48.9 Mbps

The actual traffic observed on the interface will be slightly higher because of Ethernet, IP and UDP protocol overhead.

Run the Traffic Generator

Make the script executable:

chmod +x traffic_gen.sh

Start the generator:

sudo ./traffic_gen.sh

Example output:

Started 10 UDP streams (~5 Mbps each) to 203.0.113.10
Press Ctrl+C to stop

Stop the Traffic

Press Ctrl+C to stop the test. The cleanup function keeps the process IDs of all started nping processes and terminates them automatically:

Stopping traffic...

How the Multiple Streams Work

Each nping process uses a randomly generated UDP source and destination port:

SPORT=$((1024 + RANDOM % 50000))
DPORT=$((1024 + RANDOM % 50000))

This creates multiple separate UDP flows instead of sending all traffic using the same source and destination port combination. This is useful when testing ECMP, LAG hashing, traffic distribution and network throughput in a controlled lab or authorized network environment.