Created
December 29, 2017 09:04
-
-
Save queercat/054a7a2f1955e9b4a7f2fd100bfa0e09 to your computer and use it in GitHub Desktop.
A simple port scanner. Written in Python.
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 argparse | |
| import socket | |
| parser = argparse.ArgumentParser(description='Query a host address of all ports, returns open or closed.') | |
| parser.add_argument('-u', type=str, help='The host address (e.g.) example.com') | |
| args = vars(parser.parse_args()) | |
| host = args['u'] | |
| for port in range(20, 1025): | |
| s = socket.socket() | |
| s.settimeout(.1) #Low as possible to still get a correct resp. | |
| try: | |
| s.connect((host, port)) #Try to connect. | |
| except Exception: | |
| s.close() | |
| print 'Port [%d] is closed.' % (port) | |
| continue #Do nothing! | |
| else: | |
| s.close() | |
| print 'Port [%d] is open.' % (port) #Print the port works! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment