Giter Club home page Giter Club logo

Comments (4)

mikepharesjr avatar mikepharesjr commented on August 24, 2024

Rough draft of splitting your md output

using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;

namespace File_Folder_Helper.Day;

internal static partial class Helper20231205
{

    [GeneratedRegex(@"[\\,\/,\:,\*,\?,\"",\<,\>,\|]")]
    private static partial Regex WindowsSafe();

    private static string? GetStrippedIPV4(string[] segments)
    {
        string? result = null;
        string[] subSegments;
        foreach (string segment in segments)
        {
            subSegments = segment.Split('.');
            if (subSegments.Length != 4)
                continue;
            if (!subSegments.All(l => int.TryParse(l, out _)))
                continue;
            result = segment.Replace(".", string.Empty);
        }
        return result;
    }

    private static string? GetStrippedMacAddress(string[] segments)
    {
        string? result = null;
        foreach (string segment in segments)
        {
            if (segment.Length != 17)
                continue;
            if (segment[2] is not ':' or '-' || segment[5] is not ':' or '-' || segment[8] is not ':' or '-' || segment[11] is not ':' or '-' || segment[14] is not ':' or '-')
                continue;
            result = $"{segment[0]}{segment[1]}{segment[3]}{segment[4]}{segment[6]}{segment[7]}{segment[9]}{segment[10]}{segment[12]}{segment[13]}{segment[15]}{segment[16]}";
        }
        return result;
    }

    internal static void SplitMarkdownFile(ILogger<Worker> logger, List<string> args)
    {
        string[] lines;
        string? fileName;
        string[] segments;
        string checkFileName;
        string? strippedIpV4;
        string? strippedMacAddress;
        List<string> collection = [];
        string sourceDirectory = args[0];
        if (!Directory.Exists(sourceDirectory))
            throw new Exception(sourceDirectory);
        string outputDirectory = Path.Combine(sourceDirectory, "Output");
        if (!Directory.Exists(outputDirectory))
            _ = Directory.CreateDirectory(outputDirectory);
        string[] files = Directory.GetFiles(args[0], args[2], SearchOption.TopDirectoryOnly);
        foreach (string file in files)
        {
            fileName = null;
            collection.Clear();
            lines = File.ReadAllLines(file);
            foreach (string line in lines)
            {
                collection.Add(line);
                if (line.Length > 0 && line[0] == '#' && line.StartsWith("## "))
                {
                    segments = line.Split(' ');
                    strippedIpV4 = GetStrippedIPV4(segments);
                    strippedMacAddress = GetStrippedMacAddress(segments);
                    if (strippedMacAddress is null && strippedIpV4 is null)
                        fileName = $"{WindowsSafe().Replace(line[3..], "-").Trim()}.md";
                    else if (strippedMacAddress is null)
                    {
                        fileName = $"{strippedIpV4}.md";
                        collection.Insert(0, string.Empty);
                        collection.Insert(0, $"# {fileName}");
                    }
                    else
                    {
                        fileName = $"{strippedMacAddress}.md";
                        collection.Insert(0, string.Empty);
                        collection.Insert(0, $"# {fileName}");
                    }
                }
                if (fileName is null || line != "----")
                    continue;
                logger.LogInformation("{fileName} created", fileName);
                checkFileName = Path.Combine(outputDirectory, fileName);
                if (File.Exists(checkFileName))
                    File.Delete(checkFileName);
                File.WriteAllLines(checkFileName, collection);
                collection.Clear();
            }
        }
    }

}

from nmap-formatter.

vdjagilev avatar vdjagilev commented on August 24, 2024

Hey @mikepharesjr , thanks for taking an interest in the project. I actually think of something more simpler than that. Maybe something that can be solved with simple command calls:

hosts=$(cat nmap.xml | nmap-formatter json | jq -r '[select-only-hostnames-from-json]' | uniq)

# for each host
cat nmap.xml | nmap-formatter md --filter-host-addr $host > host_$host.md

I think I can introduce something like --filter-[entity-name], what do you think?

from nmap-formatter.

mikepharesjr avatar mikepharesjr commented on August 24, 2024

Hello @vdjagilev, thank you for reviewing the request.
My rough draft to parse the current output is missing updating the toc markdown to point to the new file.
Yes, I think what you are purposing would work.
Would this also work with MAC address?

from nmap-formatter.

vdjagilev avatar vdjagilev commented on August 24, 2024

@mikepharesjr yeah, I think it's possible to implement it with something like this --filter-mac "00:AB:BC..."

from nmap-formatter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.