Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add port-sniffer #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions submissions/oleksii-manzik/port-sniffer/port-sniffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import click
import socket


@click.command()
@click.option('--host', default=False,
help='Host. Can be IP address or domain name. Mandatory argument')
@click.option('--ports', default='1-65535',
help='Ports range in format like 1-100. Default value is 1-65535')
def main(host, ports):
"""Finds available ports on specific host. Timeout set to 300ms"""

# if no host specified close the program
if not host:
print('Please enter host address')
exit(-1)

# convert ports argument value to range
port_left_lim = int(ports.split('-')[0])
port_right_lim = int(ports.split('-')[1])
ports = range(port_left_lim, port_right_lim + 1)

available_ports = get_ports(host, ports)
if available_ports:
print('\n', ','.join(available_ports), ' ports are opened')
else:
print('No port available')


def get_ports(host, ports):
# list to save available ports
buffer = []

for port in ports:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.3)
try:
con = s.connect((host, port))
except socket.timeout:
continue
else:
# if connection was successful save port
buffer.append(str(port))
print('.', end='', flush=True)
return buffer


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions submissions/oleksii-manzik/port-sniffer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Click==7.0