Looking into some python recently and going through many code books, gonna start keeping up with useful bits here!
#!/usr/bin/python
import optparse
from socket import *
from threading import *
screenLock = Semaphore(value=1)
def connScan(targetHost, targetPort):
try:
connSkt = socket(AF_INET, SOCK_STREAM)
connSkt.connect((targetHost, targetPort))
connSkt.send('grab\r\n')
results = connSkt.recv(100)
screenLock.acquire()
print ':) %d/tcp open' % targetPort
print ':) ' + str(results)
except:
screenLock.acquire()
print ':( %d/tcp closed' % targetPort
finally:
screenLock.release()
connSkt.close()
def portScan(targetHost, targetPorts):
try:
targetIP = gethostbyname(targetHost)
except:
print ":( Cannot resolve '%s': Unknown host" %targetHost
return
try:
targetName = gethostbyaddr(targetIP)
print '\n:) Scan Results for: ' + targetName[0]
except:
print '\n:) Scan Results for: ' + targetIP
setdefaulttimeout(1)
for targetPort in targetPorts:
t = Thread(target=connScan,args=(targetHost,int(targetPort)))
t.start()
def main():
parser = optparse.OptionParser('usage %prog '+\
'-H <target host> -p <target port>')
parser.add_option('-H', dest='targetHost', type='string',\
help='specify target host')
parser.add_option('-p', dest='targetPort', type='string',\
help='specify target port[s] separated by comma')
(options, args) = parser.parse_args()
targetHost = options.tgtHost
targetPorts = str(options.tgtPort).split(',')
if (targetHost == None) | (targetPorts[0] == None):
print parser.usage
exit(0)
portScan(targetHost, targetPorts)
if __name__ == '__main__':
main()