#! /usr/bin/perl -s use Net::RawSock; use NetPacket::IP; use NetPacket::TCP; use Time::HiRes qw(usleep gettimeofday tv_interval); use feature qw/say/; use Benchmark; if(!defined $n or !defined $secs ) { print "Usage: ./packgen.pl -n= -secs= \n"; exit; } print "\nCreating Packets ...."; open (FILE, '/home/tahir/Downloads/packet_generator.txt'); chomp (@lines = ()); close(FILE); ## Create IP my $ip_obj = NetPacket::IP->decode(''); ## Init IP $ip_obj->{ver} = 4; $ip_obj->{hlen} = 5; $ip_obj->{tos} = 0; $ip_obj->{id} = 0x1d1d; $ip_obj->{ttl} = 0x5a; $ip_obj->{flags} = 2; $ip_obj->{len} = 84; ## Create TCP my $tcp_obj = NetPacket::TCP->decode(''); ## Init TCP $tcp_obj->{hlen} = 5; $tcp_obj->{winsize} = 0x8e30; $tcp_obj->{seqnum} = 0xFFFF; $tcp_obj->{acknum} = 0xCCCC; $tcp_obj->{flags} = SYN; $tcp_obj->{data} =ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn; ## Assemble $ip_obj->{proto} = 6; $ip_obj->{data} = $tcp_obj->encode($ip_obj); $num =0; for ($num = 0; $num<=@lines; $num++) { $line=$lines[$num]; #for every line in the lines #build packet my($sip, $sport, $dip, $dport) = split(/\s+/, $line); $ip_obj->{src_ip} = $sip; $tcp_obj->{src_port} = $sport; $ip_obj->{dest_ip} = $dip; $tcp_obj->{dest_port} = $dport; #print ": $sip\t"; #print ": $sport\t"; #print " : $dip\t"; #print ": $dport\n"; ## Create RAW @packets[$num] = $ip_obj->encode; #say $lines[$j]; #add packet to a vector of packets #end for #Now we have an array of packets } #you want n pkt/sec for 10 seconds, so #you want n/1000 pkt per 1/1000 sec $num=0; $count = 0; $pkts_per_ms = $n / 1000; $start = time(); #[Time::HiRes::gettimeofday]; $dim= scalar(@lines); # $TEST=$packets[0]; while ( ((time()-$start)<($secs)) || ($count < ($n*$secs))) { $before = [Time::HiRes::gettimeofday]; for ($i=$pkts_per_ms;$i>0;$i--) { $num=($num+1)% $dim ; #if now packets is the array of packets,you will send Net::RawSock::write_ip($packets[$num]); # Net::RawSock::write_ip($TEST); } while (tv_interval($before)<0.001) { #print tv_interval($before), " "; } $count = $count + $pkts_per_ms; } print "TIME: ", time()-$start, "\n"; #print "You took ", tv_interval($start), " seconds for sending $count packets.\n"; print "You took ", time() - $start, " seconds for sending $count packets.\n";