import argparse
import socket
import sys
import time
import urllib2
import ftplib

#Description
parser = argparse.ArgumentParser(description='FTP Banner Grab.')

#Constants
PORT = 21
BUFFERSIZE = 1024
TIMEOUT = 15

#Arguments
parser.add_argument('-H','--host', help='[STRING] Target Address. Single Scan')
parser.add_argument('-p','--port',help='[INT] Target Port', default=PORT)
parser.add_argument('-x','--proxy',help='[STRING] Proxy address. ip:port')
parser.add_argument('-t','--timeout',help='[INT] Time to wait on a requisition', default=TIMEOUT)
parser.add_argument('-d','--delay',help='[INT]Delay on scan. Works only in Scan List')
parser.add_argument('-l','--list',help='[STRING] Scan Based on List. ')
parser.add_argument('-b','--buffersize', help='[INT] Buffersize in Bytes. Specific the size of read when try to access.. ', default=BUFFERSIZE)

args = parser.parse_args()


#Single Scan
def scanFTP():
	try:
		socket.setdefaulttimeout(int(args.timeout))
		s = socket.socket()
		s.connect((args.host, int(args.port)))
		ans = s.recv(int(args.buffersize))
		print "[Open] - "+args.host+"\n"
		if str(ans) =="":
			print "FTP Server Founded, but can't grab the banner"
		else:
			print str(ans)
	except socket.error, v:
		print "[ X ] - "+args.host+"\n"+ str(v)

#Scan from a host list
def scanList():
	fd = open(args.list, "r")
	for line in fd.readlines():
		hostList = line.strip().split(':')
		try:
			socket.setdefaulttimeout(int(args.timeout))
			s = socket.socket()
			s.connect((hostList[0], args.port))
			ans = s.recv(int(args.buffersize))
			print "[Open] - "+hostList[0]
			if str(ans) =="":
				print "FTP Server Founded, but can't grab the banner"
			else:
				print " |_ "+str(ans)	
		except socket.error, v:
			print "["+str(v.strerror)+"] - "+hostList[0]
		
		if args.delay:
			time.sleep(int(args.delay))
#sys.exit(1)

def main():

	#Call ScanList if -l
	if args.list:
		scanList()

	#Call single scan if -H	
	elif args.host:
		scanFTP()

	#elif (args.list or args.host) is None:
	#	parser.print_help()

if __name__ == "__main__":
	main()
