BASH   52

flood

Guest on 24th November 2022 03:21:41 PM

  1. #!/bin/sh
  2.  
  3. packet=/tmp/udp
  4.  
  5. if [ -z "$2" ]; then
  6.   echo "Usage: flood payload seconds [iface]">&2
  7.   exit 1
  8. fi
  9.  
  10. payload="$1"
  11. ip="10.0.0.2"
  12. sec="$2"
  13. target="$3"
  14. len=$(($payload + 14 + 20 + 8))
  15. l2len=$(($len+8+4+12))
  16.  
  17. set -f
  18. set `arp -n $ip`
  19.  
  20. case "$4" in
  21. no|*incomplete*)
  22.         echo "$ip: looking for MAC (not cached)"
  23.         ping -W 1000 -c1 -q $ip >/dev/null 2>&1
  24.         set `arp -n $ip`
  25.         ;;
  26. esac
  27.  
  28. if [ "$4" = no ]; then
  29.         echo "$ip: MAC not found, assume it's reachable through gateway"
  30.         eval `route -n get $ip | awk '/interface:/ {printf "iface=%s\n", $2;}'`
  31. else
  32.         iface="$6"
  33. fi
  34.  
  35. echo "$ip: reachable through interface: $iface"
  36.  
  37. fakeip=no
  38. if [ "$4" = '(incomplete)' ]; then
  39.   echo "$ip: MAC not found at $iface, using faked MAC"
  40.   arp -S $ip 01:01:01:01:01:01 && fakeip=yes
  41. fi
  42.  
  43. sysctl_rx() {
  44. #  sysctl dev.$1 | awk '/\.rx_frames_/ { s+=$2 } END {print s}'
  45. #  sysctl dev.$1 | awk '/\.mac_stats.good_pkts_recvd/ { s+=$2 } END {print s}'
  46. #  sysctl dev.$1 | awk '/\.mac_stats.total_pkts_recvd/ { s+=$2 } END {print s}'
  47.   sysctl dev.$1.mac_stats.total_pkts_recvd | awk '{ print $2 }'
  48. }
  49.  
  50. sysctl_tx() {
  51.   sysctl dev.$1.mac_stats.total_pkts_txd | awk '{ print $2 }'
  52. }
  53.  
  54. mk_packet() {
  55.   echo "$ip: preparing a packet for flood..."
  56.   rm -f $packet.pcap
  57.   [ -n "$target" ] && iface="$target"
  58.   tcpdump -w $packet.pcap -i $iface -s0 -c1 -np dst host $ip and udp port 5001 >/dev/null 2>&1 &
  59.   tpid=$!
  60.   iperf -l $payload -u -t 1 -c $ip -b 10K >/dev/null 2>&1
  61.   sleep 1
  62.   kill $tpid >/dev/null 2>&1
  63.  
  64.   set -- `ls -l $packet.pcap`
  65.   if [ -z "$5" -o "$5" -le 24 ]; then
  66.     echo $ip: cannot prepare a packet, stop. >&2
  67.     exit 1
  68.   fi
  69.  
  70.   tail -c $len $packet.pcap > $packet.raw
  71. }
  72.  
  73. mk_packet
  74. [ $fakeip = yes ] && arp -d $ip
  75.  
  76. echo "$ip: flooding through $iface: payload=$payload iplen="$(($payload+20+8))" framelen=$len l2len=$l2len"
  77.  
  78. stop_and_show() {
  79.   ngctl msg $iface:orphans stop
  80.   eval `ngctl msg $iface:orphans getstats |\
  81.     awk '/^Args:/ { print $3, $4, $16, $17 }'`
  82.   sleep 2
  83.   s_tx=$((`sysctl_tx igb.0`-$s_tx))
  84.   s_rx=$((`sysctl_rx igb.1`-$s_rx))
  85.   echo $ip: flood time was `printf "scale=3\n$tv_sec+${tv_usec}/1000000\n" | bc -l` sec, $s_tx frames sent
  86.   echo $ip: flood L2 speed was `printf "scale=3\n$s_tx/($tv_sec+${tv_usec}/1000000)\n" | bc -l` frames/sec
  87.   echo $ip: flood L2 speed was `printf "scale=3\n$l2len*$s_tx*8/($tv_sec+${tv_usec}/1000000)/1000000\n" | bc -l` Mbit/sec
  88.  
  89.   ngctl shutdown $iface:orphans
  90.   lost=`printf "scale=3\n($s_tx-$s_rx)/($tv_sec+${tv_usec}/1000000)\n" | bc -l`
  91.   echo $ip: "lost " $(($s_tx-$s_rx)) frames, $lost frames/sec, `printf "scale=3\n($s_tx-$s_rx)*100/$s_tx\n" | bc -l`\%
  92.   echo $ip: transmitted $s_tx, received $s_rx
  93. }
  94.  
  95. trap 'stop_and_show; exit 0' SIGHUP SIGINT SIGTERM
  96.  
  97. count=1000000000
  98.  
  99. if ngctl mkpeer $iface: source orphans output >/dev/null 2>&1; then
  100.   sleep 2
  101.   s_tx=`sysctl_tx igb.0`
  102.   s_rx=`sysctl_rx igb.1`
  103.   nghook $iface:orphans input < $packet.raw >/dev/null 2>&1
  104.   ngctl msg $iface:orphans start $count
  105.   sleep $sec
  106.   stop_and_show
  107.   exit 0
  108. fi
  109. echo $ip: cannot flood through interface $iface

Raw Paste


Login or Register to edit or fork this paste. It's free.