Created
March 25, 2019 00:19
-
-
Save gmelodie/d60739cad1243b76b4862a62cc5a967c to your computer and use it in GitHub Desktop.
Port scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import optparse | |
def createParser(): | |
# Create command line parser | |
parser = optparse.OptionParser('usage prog -H <target host> -p ' + \ | |
'<target port>') | |
parser.add_option('-H', dest='tgtHost', type='string',\ | |
help='Specify target host') | |
parser.add_option('-P', dest='tgtPorts', type='string', \ | |
help='Specify target ports') | |
return parser | |
def connScan(tgtHost, tgtPort): | |
sock = socket.socket() | |
try: | |
sock.connect((tgtHost, tgtPort)) | |
print("[+] Open") | |
except: | |
print("[-] Closed") | |
finally: | |
sock.send("Hello".encode()) | |
response = sock.recv(128) | |
print("[+] " + str(response)) | |
sock.close() | |
def portScan(hostname, ports): | |
try: | |
tgtHost = socket.gethostbyname(hostname) | |
except: | |
print("[-] Couldn't find host ", hostname) | |
exit(0) | |
print("[+] ", end="") | |
print(hostname, tgtHost) | |
for tgtPort in ports: | |
print("[+] Trying port", tgtPort) | |
connScan(tgtHost, int(tgtPort)) | |
def main(): | |
parser = createParser() | |
(options, args) = parser.parse_args() | |
tgtHost = options.tgtHost | |
tgtPorts = str(options.tgtPorts).split(', ') | |
if tgtHost == None or tgtPorts == None: | |
parser.print_help() | |
exit(0) | |
portScan(tgtHost, tgtPorts) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment