# # sshd.py # # Find number of ssh logins and authorised users # import re from ..formatting import * from ..util import readlog, resolve from .. import config import logging logger = logging.getLogger(__name__) def parse_log(): logger.debug("Starting sshd section") section = Section("ssh") logger.debug("Searching for matches in {0}".format(config.prefs['logs']['auth'])) matches = re.findall('.*sshd.*Accepted publickey for .* from .*', readlog(config.prefs['logs']['auth'])) # get all logins logger.debug("Finished searching for logins") users = [] # list of users with format [username, number of logins] for each item data = [] num = sum(1 for x in matches) # total number of logins for match in matches: entry = re.search('^.*publickey\sfor\s(\w*)\sfrom\s(\S*)', match) # [('user', 'ip')] user = entry.group(1) ip = entry.group(2) userhost = user + '@' + resolve(ip, fqdn=config.prefs['sshd']['resolve-domains']) users.append(userhost) logger.debug("Parsed list of authorised users") auth_data = Data(subtitle=plural('login', num) + ' from', items=users) if (len(auth_data.items) == 1): # if only one user, do not display no of logins for this user logger.debug("found " + str(len(matches)) + " ssh logins for user " + users[0]) auth_data.subtitle += ' ' + auth_data.items[0] auth_data.orderbyfreq() auth_data.truncl(config.prefs['maxlist']) logger.debug("Found " + str(len(matches)) + " ssh logins for users " + str(data)) section.append_data(auth_data) logger.info("Finished sshd section") return section