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:

#!/usr/bin/perl
##############

# udp flood.
##############
 
use Socket;
use strict;
 
if ($#ARGV != 3) {
  print "flood.pl <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";
  exit(1);
}
 
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));}

7 comments:

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

    ReplyDelete
  2. Or you can just use iperf...

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

    ReplyDelete
  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

    http://www.mjmwired.net/kernel/Documentation/networking/pktgen.txt

    ReplyDelete
  4. s/software/script

    for example: ftp://robur.slu.se/pub/Linux/net-development/pktgen-testing/examples/pktgen.conf-1-1

    ReplyDelete
  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!

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

    ReplyDelete

You don't have to log in to post a comment, but please do provide your real name/URL. Anonymous comments might get deleted.

Ivan Pepelnjak, CCIE#1354, is the chief technology advisor for NIL Data Communications. He has been designing and implementing large-scale data communications networks as well as teaching and writing books about advanced technologies since 1990. See his full profile, contact him or follow @ioshints on Twitter.