Skip to content

Commit

Permalink
Merge 04973df into d8eb46d
Browse files Browse the repository at this point in the history
  • Loading branch information
jtesta authored Oct 29, 2017
2 parents d8eb46d + 04973df commit eb37dc7
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions ssh-audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def usage(err=None):
uout.info(' -n, --no-colors disable colors')
uout.info(' -v, --verbose verbose output')
uout.info(' -l, --level=<level> minimum output level (info|warn|fail)')
uout.info(' -t, --timeout=<secs> timeout (in seconds) for connection and reading\n (default: 5)')
uout.sep()
sys.exit(1)

Expand All @@ -87,7 +88,8 @@ def __init__(self, host=None, port=22):
self.ipvo = () # type: Sequence[int]
self.ipv4 = False
self.ipv6 = False

self.timeout = 5.0

def __setattr__(self, name, value):
# type: (str, Union[str, int, bool, Sequence[int]]) -> None
valid = False
Expand Down Expand Up @@ -124,6 +126,11 @@ def __setattr__(self, name, value):
valid = True
elif name == 'host':
valid = True
elif name == 'timeout':
value = utils.parse_float(value)
if value == -1.0:
raise ValueError('invalid timeout: {0}'.format(value))
valid = True
if valid:
object.__setattr__(self, name, value)

Expand All @@ -133,9 +140,9 @@ def from_cmdline(cls, args, usage_cb):
# pylint: disable=too-many-branches
aconf = cls()
try:
sopts = 'h1246p:bnvl:'
sopts = 'h1246p:bnvl:t:'
lopts = ['help', 'ssh1', 'ssh2', 'ipv4', 'ipv6', 'port',
'batch', 'no-colors', 'verbose', 'level=']
'batch', 'no-colors', 'verbose', 'level=', 'timeout=']
opts, args = getopt.getopt(args, sopts, lopts)
except getopt.GetoptError as err:
usage_cb(str(err))
Expand Down Expand Up @@ -165,6 +172,8 @@ def from_cmdline(cls, args, usage_cb):
if a not in ('info', 'warn', 'fail'):
usage_cb('level {0} is not valid'.format(a))
aconf.level = a
elif o in ('-t', '--timeout'):
aconf.timeout = float(a)
if len(args) == 0:
usage_cb()
if oport is not None:
Expand Down Expand Up @@ -1586,16 +1595,15 @@ def _resolve(self, ipvo):
out.fail('[exception] {0}'.format(e))
sys.exit(1)

def connect(self, ipvo=(), cto=3.0, rto=5.0):
# type: (Sequence[int], float, float) -> None
def connect(self, ipvo, timeout):
# type: (Sequence[int], float) -> None
err = None
for af, addr in self._resolve(ipvo):
s = None
try:
s = socket.socket(af, socket.SOCK_STREAM)
s.settimeout(cto)
s.settimeout(timeout)
s.connect(addr)
s.settimeout(rto)
self.__sock = s
return
except socket.error as e:
Expand Down Expand Up @@ -2159,6 +2167,14 @@ def parse_int(v):
except: # pylint: disable=bare-except
return 0

@staticmethod
def parse_float(v):
# type: (Any) -> float
try:
return float(v)
except: # pylint: disable=bare-except
return -1.0


def audit(aconf, sshv=None):
# type: (AuditConf, Optional[int]) -> None
Expand All @@ -2167,7 +2183,7 @@ def audit(aconf, sshv=None):
out.level = aconf.level
out.use_colors = aconf.colors
s = SSH.Socket(aconf.host, aconf.port)
s.connect(aconf.ipvo)
s.connect(aconf.ipvo, aconf.timeout)
if sshv is None:
sshv = 2 if aconf.ssh2 else 1
err = None
Expand Down

0 comments on commit eb37dc7

Please sign in to comment.