From 14e2abafc3bbd9a0018202e803d6892861268d0a Mon Sep 17 00:00:00 2001 From: ScriptTiger Date: Sat, 11 Jun 2022 14:53:00 +0800 Subject: [PATCH] Initial commit --- .gitattributes | 2 ++ .gitignore | 1 + Build.cmd | 42 +++++++++++++++++++++++++++++ LICENSE | 21 +++++++++++++++ README.md | 15 +++++++++++ include_other.go | 4 +++ include_windows.go | 4 +++ oui_standardize.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 156 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 Build.cmd create mode 100644 LICENSE create mode 100644 README.md create mode 100644 include_other.go create mode 100644 include_windows.go create mode 100644 oui_standardize.go diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6211195 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/Release/* \ No newline at end of file diff --git a/Build.cmd b/Build.cmd new file mode 100644 index 0000000..d02a23d --- /dev/null +++ b/Build.cmd @@ -0,0 +1,42 @@ +@echo off + +if not exist "Release" md "Release" + +set APP=oui_standardize + +set GOARCH=amd64 +call :Build_OS + +set GOARCH=386 +call :Build_OS + +pause +exit /b + +:Build_OS + +set GOOS=windows +set EXT=.exe +set INC=include_windows.go +call :Build + +set GOOS=linux +set EXT= +set INC=include_other.go +call :Build + +if %GOARCH% == 386 exit /b + +set GOOS=darwin +set EXT=.app +set INC=include_other.go +call :Build + +exit /b + +:Build + +echo Building %APP%_%GOOS%_%GOARCH%%EXT%... +go build -ldflags="-s -w" -o "Release/%APP%_%GOOS%_%GOARCH%%EXT%" %APP%.go %INC% + +exit /b \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..938e64a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 ScriptTiger + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4870c45 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/thescripttiger%40gmail.com) + +# oui_standardize +oui_standardize standardizes IEEE MAC address block files (CSV files) containing OUI records by importing an arbitrary number of said files, sorting them by MAC address prefix, and then outputting the results to standard output (stdout). Curly brackets are used as delimiters and multi-line fields are reduced to single lines in order to allow scripts to more easily parse the records with as little overhead as possible. + +Usage: `oui_standardize [] [] []...` + +# More About ScriptTiger + +For more ScriptTiger scripts and goodies, check out ScriptTiger's GitHub Pages website: +https://scripttiger.github.io/ + +[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MZ4FH4G5XHGZ4) + +Donate Monero (XMR): 441LBeQpcSbC1kgangHYkW8Tzo8cunWvtVK4M6QYMcAjdkMmfwe8XzDJr1c4kbLLn3NuZKxzpLTVsgFd7Jh28qipR5rXAjx diff --git a/include_other.go b/include_other.go new file mode 100644 index 0000000..f5f2f0a --- /dev/null +++ b/include_other.go @@ -0,0 +1,4 @@ +package main + +//Set line endings to LF +const eol = "\n" diff --git a/include_windows.go b/include_windows.go new file mode 100644 index 0000000..cbf2dea --- /dev/null +++ b/include_windows.go @@ -0,0 +1,4 @@ +package main + +//Set line endings to CRLF +const eol = "\r\n" diff --git a/oui_standardize.go b/oui_standardize.go new file mode 100644 index 0000000..0f6cf89 --- /dev/null +++ b/oui_standardize.go @@ -0,0 +1,67 @@ +package main + +import ( + "os" + "io" + "encoding/csv" + "strings" + "sort" +) + +var data []string + +func readCSV(file string) { + + raw, err := os.Open(file) + if err != nil {panic(err)} + defer raw.Close() + + reader := csv.NewReader(raw) + + // Skip header line + reader.Read() + + for { + // Read CSV line + line, err := reader.Read() + + // Validate line + if err == io.EOF {break} + if err != nil {panic(err)} + + // Remove multi-line strings from addresses + line[3] = strings.Replace(line[3], "\r", "", -1) + line[3] = strings.Replace(line[3], "\n", " ", -1) + + // Null-separated strings, starting with mac prefix + data = append(data, strings.Join([]string{line[1], line[0], line[2], line[3]}, "\x00")) + } + +} + +func help() { + + os.Stdout.WriteString("Usage: oui_standardize [] [] []...\n") + os.Exit(0) + +} + +func main() { + + //Display help and exit if not enough arguments + if len(os.Args) < 2 {help()} + + // Treat arguments as file paths and read all files into data + for i := 1; i < len(os.Args); i++ {readCSV(os.Args[i])} + + // Sort data + sort.Strings(data) + + // Format and output + for _, line := range data { + fields := strings.Split(line, "\x00") + os.Stdout.WriteString( + "{"+fields[1]+"}{"+fields[0]+"}{"+fields[2]+"}{"+fields[3]+"}"+eol) + } + +} \ No newline at end of file