Brijesh's Git Server — network-scan @ caed91fccd3ad2409debaa1bc530d686969e8071

my own tool to scan for devices in local network, beyond just arp -a

oui/oui-import.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
package oui

import (
	"bufio"
	"fmt"
	"network-scan/database"
	"os"
	"regexp"
)

func ScanDataFromTextFile(filePath string, db *database.Database) {
	file, err := os.Open(filePath)
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	regexBase16 := regexp.MustCompile(`([0-9A-Fa-f]{6})\s+\(base 16\)\s+(.*)`)

	var organisation string
	index := 1
	for scanner.Scan() {
		line := scanner.Text()

		if matches := regexBase16.FindStringSubmatch(line); matches != nil {
			macPrefix := matches[1]
			organisation = matches[2]

			err := db.InsertOUI(macPrefix, organisation)
			if err != nil {
				fmt.Printf("%d) Error inserting this record => Organisation: %s Assignment Base16: %s\n", index, organisation, macPrefix)
				fmt.Println("Error inserting OUI:", err)
			} else {
				fmt.Printf("Inserted %d records successfully\n", index)
			}

			index++
		}
	}

	if err := scanner.Err(); err != nil {
		fmt.Println("Error reading file:", err)
	}
}