#!/usr/bin/env python3
from argparse import ArgumentParser
from netaddr import IPSet
import ftplib
import threading

argparser = ArgumentParser()
argparser.add_argument('targets', nargs='+')
argparser.add_argument('-t', '--threads',
                        action='store', default=20, type=int, dest='maxThreads', help='number of threads to use, default is 20')
argparser.add_argument('-w', '--wait',
                        action='store', default=2, type=int, dest='timeout', help='seconds to wait before timeout, default is 2')
# TODO: add option for specifing a file with hosts
# argparser.add_argument('-f', '--file',
#                        action='store', dest='hostlist', help='optional file with target hosts')
args = argparser.parse_args()

# add the given targets to a IPSet
targetIPSets = IPSet()
for target in args.targets:
	targetIPSets.add(target)

# render the IPSet to a list with individual IPs
targetlist = list()
for ip in targetIPSets:
	targetlist.append(str(ip))

def tryFtpConnect(host):	
	ftp = ftplib.FTP()
	try:
		ftp.connect(host=host, timeout=args.timeout)
		if '230' in ftp.login():
			print(host)
			ftp.quit()
	except ftplib.all_errors:
		print(host + " ERROR")


for host in targetlist:
	while(threading.activeCount() >= args.maxThreads):
		continue
	threading.Thread(target=tryFtpConnect, args=(host,)).start()

@@ -78,16 +78,43 @@
 t IPSet%0A
+from random import shuffle%0A
 import f
@@ -137,16 +137,206 @@
 eading%0A%0A
+def tryFtpConnect(host):%09%0A%09ftp = ftplib.FTP()%0A%09try:%0A%09%09ftp.connect(host=host, timeout=args.timeout)%0A%09%09if '230' in ftp.login():%0A%09%09%09print(host)%0A%09%09%09ftp.quit()%0A%09except ftplib.all_errors:%0A%09%09pass%0A%0A
 argparse
@@ -741,16 +741,166 @@
  is 2')%0A
+argparser.add_argument('-s', '--shuffle',%0A                        action='store_true', default=False, dest='shuffle', help='shuffle the target list')%0A
 # TODO: 
@@ -1351,217 +1351,47 @@
 ))%0A%0A
-def tryFtpConnect(host):%09%0A%09ftp = ftplib.FTP()%0A%09try:%0A%09%09ftp.connect(host=host, timeout=args.timeout)%0A%09%09if '230' in ftp.login():%0A%09%09%09print(host)%0A%09%09%09ftp.quit()%0A%09except ftplib.all_errors:%0A%09%09print(host + %22 ERROR%22
+if args.shuffle:%0A%09shuffle(targetlist
 )%0A%0A
-%0A
 for 
@@ -1473,16 +1473,16 @@
 ontinue%0A
-
 %09threadi
@@ -1531,12 +1531,14 @@
 ,)).start()%0A
+%0A%0A

add shuffle argument