Building network automation solutions

9 module online course

Start now!

UDP flood in Perl

If you'll ever find yourself in a situation where you'll need UDP flooding (serial line or device stress testing) but won't have a dedicated flood program available (they're usually just a few click away if you consult uncle Google), here's a Perl version of UDP flood:

# udp flood.
use Socket;
use strict;
if ($#ARGV != 3) {
  print " <ip> <port> <size> <time>\n\n";
  print " port=0: use random ports\n";
  print " size=0: use random size between 64 and 1024\n";
  print " time=0: continuous flood\n";
my ($ip,$port,$size,$time) = @ARGV;
my ($iaddr,$endtime,$psize,$pport);
$iaddr = inet_aton("$ip") or die "Cannot resolve hostname $ip\n";
$endtime = time() + ($time ? $time : 1000000);
socket(flood, PF_INET, SOCK_DGRAM, 17);

print "Flooding $ip " . ($port ? $port : "random") . " port with " .
  ($size ? "$size-byte" : "random size") . " packets" .
  ($time ? " for $time seconds" : "") . "\n";
print "Break with Ctrl-C\n" unless $time;
for (;time() <= $endtime;) {
  $psize = $size ? $size : int(rand(1024-64)+64) ;
  $pport = $port ? $port : int(rand(65500))+1;
  send(flood, pack("a$psize","flood"), 0, pack_sockaddr_in($pport, $iaddr));}

We migrated our blog a few days ago, and the commenting functionality is not there yet. In the meantime enjoy the older comments, or find our content on LinkedIn and comment there.


  1. If you need to flood low-speed ling, ping -f is always handy. :)

  2. Or you can just use iperf...

    iperf -u -b #[KM] -c [host]

  3. Hey, it isn't the most efficient way to flood out some destination with as many packets per second as the machine can... when you really want to do that efficiently you should use linux and select kernel option: CONFIG_NET_PKTGEN and then use software that will use this feature. Take a look at a documentation Documentation/networking/pktgen.txt

  4. s/software/script

    for example:

  5. As I wrote ... just in case you don't have a dedicated program handy, use this PERL script. Obviously there are MANY more efficient ways to implement flood than an interpreted language :)

    And thanks for the pktgen link!

  6. Wie kann ich das nun auf einem Windows 7 Computer ausführen?