snerstack / sner-monorepo

sner -- slow network recon
1 stars 0 forks source link

add resolve hostnames fix script #84

Open bodik opened 2 months ago

bodik commented 2 months ago
#!/usr/bin/python3
# report helper when nessus scan performed without 'designate hosts by hostname'

import logging
from argparse import ArgumentParser

from sner.server.app import create_app
from sner.server.extensions import db
from sner.server.storage.models import Host

logger = logging.getLogger()  # pylint: disable=invalid-name
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)

import csv
import re
import sys
from pathlib import Path
from pprint import pprint

COLID='Zdroje problému (hostnames)'
#COLID='Tag'

writer = csv.DictWriter(sys.stdout, ['xxx'], quoting=csv.QUOTE_ALL)

def get_hostname(addr):
    host = Host.query.filter(Host.address==addr).one()
    return host.hostname or host.address

def update(afile):
    reader = csv.DictReader(Path(afile).open())
    for line in reader:
        if not line[COLID]:
            writer.writerow({'xxx': ''})
            continue

        buf = []
        items = line[COLID].splitlines()
        for item in items:
            #pprint(line['Zdroje problému (hostnames)'])
            if match := re.match(r'(?P<addr>\d.*):(?P<port>\d+)', item):
                buf.append(f"{get_hostname(match.group('addr'))}:{match.group('port')}")
            elif match := re.match(r'(?P<addr>\d+\.\d+\.\d+\.\d+)', item):
                buf.append(f"{get_hostname(match.group('addr'))}")
            else:
                buf.append(f"{item}")

        writer.writerow({'xxx': '\n'.join(buf)})

def main():
    """main"""

    parser = ArgumentParser()
    parser.add_argument('afile')
    args = parser.parse_args()

    with create_app().app_context():
        update(args.afile)

if __name__ == '__main__':
    main()
bodik commented 2 months ago

V tom skriptiku na dogenerovani hostnames by chtel udelat uniq, tady jsem to upravil rucne.