Skip to content

Commit

Permalink
py35
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Aug 22, 2018
1 parent 5553e31 commit b99df01
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# this also works great on osx, but I disabled osx to conserve Travis-CI resources.
language: python
sudo: required
dist: xenial
group: travis_latest

git:
Expand All @@ -10,7 +8,7 @@ git:

python:
- 3.6
- 3.7
- 3.5

os:
- linux
Expand Down
32 changes: 12 additions & 20 deletions findssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def run(port: int=PORT, service: str='', timeout: float=TIMEOUT,
baseip: Union[str, ip.IPv4Address]=None, debug: bool=False):
tic = time()

ownip: Union[ip.IPv4Address, ip.IPv6Address]
if not baseip:
ownip = getLANip()
print('own address', ownip)
Expand All @@ -37,7 +36,7 @@ def run(port: int=PORT, service: str='', timeout: float=TIMEOUT,

assert isinstance(ownip, ip.IPv4Address)

net: ip.IPv4Network = netfromaddress(ownip)
net = netfromaddress(ownip)

servers = scanhosts(net, port, service, timeout, debug)
print('\n*** RESULTS ***')
Expand All @@ -59,16 +58,16 @@ def getLANip() -> Union[ip.IPv4Address, ip.IPv6Address]:
except OSError:
s.connect(('8.8.8.8', 80)) # for BSD/Mac

name: str = s.getsockname()[0]
name = s.getsockname()[0]

return ip.ip_address(name)


# %% (2) scan subnet for SSH servers
def isportopen(host: ip.IPv4Address, port: int, service: str,
timeout: float=TIMEOUT,
verbose: bool=True) -> Union[bool, None]:
h: str = host.exploded
verbose: bool=True) -> bool:
h = host.exploded

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(timeout) # seconds
Expand All @@ -84,38 +83,31 @@ def isportopen(host: ip.IPv4Address, port: int, service: str,

except (ConnectionRefusedError, socket.timeout, socket.error):
logging.info('no connection to {} {}'.format(h, port))
return None
return False


def validateservice(service: str, h: str, b: bytes) -> bool:
if not b: # empty reply
return False
# %% non-empty reply
u: Union[str, bytes]
try:
"""
splitlines is in case the ASCII/UTF8 response is less than 32 bytes,
hoping server sends a \r\n
"""
u = b.splitlines()[0].decode('utf-8')
print('\n', u)
except UnicodeDecodeError:
"""
must not have been utf8 encoding..., maybe latin1 or something else..
"""
u = b
print('\n', b)
return False

print('\n', u)
# %% optional service validation
val: bool = False
if service:
try:
if service in u.lower():
val = True
except UnicodeDecodeError:
logging.error('unable to decode response'.format(h))
val = False
else:
val = True
val = True
if service and service not in u.lower():
val = False

return val

Expand Down Expand Up @@ -144,7 +136,7 @@ def scanhosts(net: ip.IPv4Network,

print('searching', net)

hosts: List[ip.IPv4Address] = list(net.hosts())
hosts = net.hosts()

if debug:
servers = [h for h in hosts if isportopen(h, port, service, timeout)]
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = findssh
version = 1.0.8
version = 1.0.9
author = Michael Hirsch, Ph.D.
url = https://github.com/scivision/findssh
description = find open servers on your IPv4 subnet, e.g. SSH
Expand All @@ -13,6 +13,7 @@ classifiers =
Intended Audience :: Information Technology
Intended Audience :: System Administrators
Operating System :: OS Independent
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Topic :: System :: Networking
Expand All @@ -22,7 +23,7 @@ long_description = file: README.md
long_description_content_type = text/markdown

[options]
python_requires = >= 3.6
python_requires = >= 3.5
setup_requires =
setuptools >= 38.6
pip >= 10
Expand Down
10 changes: 8 additions & 2 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/usr/bin/env python
import pytest
import subprocess
import findssh


def test_all():
def test_script():
subprocess.check_call(['findssh'])


def test_mod():
findssh.run()



if __name__ == '__main__':
pytest.main()
pytest.main(['-xv', __file__])

0 comments on commit b99df01

Please sign in to comment.