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

return array of dicts for result #1

Open
wants to merge 2 commits 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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.pyc
beard_portscan.egg-info/*
dist
env
build
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.md
include LICENSE
15 changes: 11 additions & 4 deletions readme.md → README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# PortScan
PyPi Project: beard-portscan

![PyPI version](http://img.shields.io/pypi/v/portscan.svg)   ![Python 3.x](http://img.shields.io/badge/Python-3.x-green.svg)   ![PyPI license](https://img.shields.io/github/license/mashape/apistatus.svg)   [![Downloads](https://pepy.tech/badge/portscan)](https://pepy.tech/project/portscan)
![PyPI](https://img.shields.io/pypi/v/beard-portscan)   ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/beard-portscan)   ![PyPI - License](https://img.shields.io/pypi/l/beard-portscan)

PortScan is a *light-weight* command line utility that allows user to conduct scanning over a range of IP addresses and port ranges with multi-threading.
- 0.1.0: Initial Release after Fork
- Outputs a list of dicts

Install: `pip install portscan`
Install: `pip install beard-portscan`

Upgrade: `pip install portscan --upgrade`
Upgrade: `pip install beard-portscan --upgrade`

Usage: `portscan [192.168.1.0/24] [-p 22,80-200 [-t 100 [-w 1 [-e]]]]`

## Forked from [Aperocky/PortScan](https://github.com/aperocky/PortScan)
### All Information below this line is from the original REPO.

PortScan is a *light-weight* command line utility that allows user to conduct scanning over a range of IP addresses and port ranges with multi-threading.

*New in version 0.2.1:*

![Simple Command](/images/Demo_3.png)
Expand Down
2 changes: 1 addition & 1 deletion patch_notes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Version 0.3.0: Fixed major bug relating to premature ending of queue. Force ending of queue to be preceded by stopiteration of the generator.
Version 0.1.0: Forked from Aperocky/PortScan, Added output to list of dicts
32 changes: 26 additions & 6 deletions portscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import os
import time
import json
try:
from queue import Queue
except ImportError:
Expand All @@ -13,7 +14,7 @@
# Expand thread number possible with extended FILE count.
# This remain as low as 2048 due to macOS secret open limit, unfortunately
resource.setrlimit(resource.RLIMIT_NOFILE, (2048, 2048))

json.dumps
# A multithreading portscan module
class PortScan:

Expand All @@ -22,7 +23,7 @@ class PortScan:
BLOCK_24 = r'^(?:\d{1,3}\.){3}0\/24$'
GROUPED_IP = r'^\[.*\]$'

def __init__(self, ip_str, port_str = None, thread_num = 500, show_refused=False, wait_time=3):
def __init__(self, ip_str, port_str = None, thread_num = 500, show_refused=False, wait_time=3,stdout=True):
self.ip_range = self.read_ip(ip_str)
if port_str is None:
self.ports = [22, 23, 80]
Expand All @@ -37,6 +38,8 @@ def __init__(self, ip_str, port_str = None, thread_num = 500, show_refused=False
self.show_refused = show_refused
self.wait_time = wait_time
self.queue_status = False
self.output = []
self.stdout = stdout

# Read in IP Address from string.
def read_ip(self, ip_str):
Expand Down Expand Up @@ -112,25 +115,42 @@ def run(self):
# Before master thread finishes.
time.sleep(0.1)
self.q.join()
# If stdout True, return None, else return self.output
output = None if self.stdout else self.output
return output

def worker(self):
# Worker threads that take ports from queue and consume it
while True: # never stop working!
work = self.q.get()
self.ping_port(*work)
self.output.append(self.ping_port(*work))
self.q.task_done()

def ping_port(self, ip, port):
output = {}
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self.wait_time)
status = sock.connect_ex((ip, port))
if status == 0:
with self.lock:
print('{}:{} OPEN'.format(ip, port))
if self.stdout:
stdout = '{}:{} OPEN'.format(ip, port)
else:
output = {'ip': ip,'port':port,'open':True,'status':status,'errormsg':''}
elif status not in [35, 64, 65] and self.show_refused:
with self.lock:
print('{}:{} ERRNO {}, {}'.format(ip, port, status, os.strerror(status)))
return
if self.stdout:
stdout = '{}:{} ERRNO {}, {}'.format(ip, port, status, os.strerror(status))
output = {'ip': ip,'port':port,'open':False,'status':status,'errormsg':os.strerror(status)}
else:
output=False
stdout=False
if self.stdout:
if stdout:
print(stdout)
output = None
if output:
return output


def get_local_ip():
Expand Down
34 changes: 20 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

from pathlib import Path
from setuptools import find_packages, setup
dependencies = []
# read the contents of README file
this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text()
setup(
name="portscan",
version="0.3.0",
description="Simple port scanning utility at terminal",
author="Rocky Li",
author_email="aperocky@gmail.com",
license="MIT",
url="https://github.com/Aperocky/PortScan",
name='beard-portscan',
version='0.1.5',
description="Simple port scanning utility at terminal forked from Aperocky/PortScan",
author="The Bearded Tek",
author_email="kenny@beardedtek.com",
url="https://github.com/beardedtek/PortScan",
long_description=long_description,
long_description_content_type='text/markdown',
license='MIT',
project_urls={
"Bug Tracker": "https://github.com/beardedtek/PortScan/issues",
},
keywords=[
"port",
"scanner",
Expand All @@ -29,11 +35,11 @@
"Programming Language :: Python :: 3",
"Topic :: Utilities",
],
install_requires=[],
install_requires=dependencies,
py_modules=['portscan'],
entry_points={
'console_scripts': [
"portscan=portscan:main",
"portscan=portscan:main"
],
},
)