Skip to content

Instantly share code, notes, and snippets.

@TunedMystic
Created April 2, 2020 17:19
Show Gist options
  • Save TunedMystic/d8e709786578e1406faaeb40e695b7a1 to your computer and use it in GitHub Desktop.
Save TunedMystic/d8e709786578e1406faaeb40e695b7a1 to your computer and use it in GitHub Desktop.
Scan some ports
import socket
import sys
def isOpen(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
s.shutdown(2)
return True
except Exception as e:
return False
def get_arg(index, cast=None, default=None):
try:
value = sys.argv[index]
if cast:
value = cast(value)
return value
except IndexError:
return default
if __name__ == '__main__':
min_port = get_arg(1, int, 3000)
max_port = get_arg(2, int, 7000)
print('Scanning ports {} to {}'.format(min_port, max_port))
for port in range(min_port, max_port):
if isOpen('0.0.0.0', str(port)):
print('Port {} is being used'.format(port))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment