port_flood.py revision 25531ee0
1#/usr/bin/env python
2
3import sys
4import socket
5
6
7def do_flood(host, num):
8  try:
9    socket.inet_aton(host)
10    is_ip4 = True
11  except socket.error:
12    try:
13      socket.inet_pton(socket.AF_INET6, host)
14      is_ip4 = False
15    except socket.error:
16        raise Exception('Invlid ip4/6 address!')
17
18  family = socket.AF_INET if is_ip4 else socket.AF_INET6
19
20  for port in range(num):
21    sock = socket.socket(family, socket.SOCK_DGRAM)
22    sock.sendto('test', (host, port + 1))
23
24
25if __name__ == '__main__':
26  if len(sys.argv) < 2:
27    raise Exception('IP and packet count expected!')
28  do_flood(sys.argv[1], int(sys.argv[2]))
29