So I am trying to write a basic Port Scanner to learn some simple Python however i keep getting this error:
File "ports.py", line 42
def main():
^
IndentationError: unexpected unindent
It is probably something stupid but I can not find it, here is the script:
import socket
import os
import sys
portList = []
failed = 'banner grab failed'
def testConnection(ip, port):
socket.setdefaulttimeout(10)
s = socket.socket()
try:
s.connect((ip, port))
portList.append(port)
s.close()
return()
except Exception, e:
print(e)
def checkVulns(banner):
vulns = open('vulnlist','r')
for line in vulns.readlines():
if line.strip('\n') in banner:
print'Vulnerable by: '+line.strip('\n')+'' + banner.strip('\n')
def grabbanner(ip, port):
try:
socket.setdefaulttimeout(20)
s = socket.socket()
s.connect((ip, port))
try:
banner = s.recv(2000)
if banner == "":
return('No banner on first pass')
else:
return banner
except Exception, e:
print'Um well... '+ e
return failed
def main():
if len(sys.argv) == 2:
ip = sys.argv[1]
else:
print'Improper usage, proper usage: python ports.py <target>'
exit(0)
for x in range(1, 4000):
testConnection(ip, x)
print portList
for item in portList:
banner = grabbanner(ip, item)
print'Port '+str(item)+' Banner:'+ banner
if banner != failed
checkVulns(banner)
main()