go代码
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
// 定义MAC地址的正则表达式
macRegex := regexp.MustCompile(`(?i)(?:[0-9A-F]{2}[:-]){5}[0-9A-F]{2}`)
for scanner.Scan() {
line := scanner.Text()
words := strings.Fields(line)
var result []string
for _, word := range words {
if macRegex.MatchString(word) {
mac := strings.ReplaceAll(word, ":", "-")
mac = strings.ToUpper(mac)
if len(mac) >= 8 {
mac = mac[:8]
} else {
result = append(result, fmt.Sprintf("%s (Unknown)", word))
continue
}
file, err := os.Open("oui.txt")
if err != nil {
fmt.Println("Error opening file:", err)
os.Exit(1)
}
defer file.Close()
fileScanner := bufio.NewScanner(file)
var organization string
for fileScanner.Scan() {
fileLine := fileScanner.Text()
if strings.Contains(fileLine, mac) && strings.Contains(fileLine, "(hex)") {
organization = strings.TrimSpace(strings.Split(fileLine, "(hex)")[1])
break
}
}
if err := fileScanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}
if organization != "" {
result = append(result, fmt.Sprintf("%s (%s)", word, organization))
} else {
result = append(result, fmt.Sprintf("%s (Unknown)", word))
}
} else {
result = append(result, word)
}
}
fmt.Println(strings.Join(result, " "))
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Error reading standard input:", err)
os.Exit(1)
}
}
编译脚本
#!/bin/bash
export GOROOT=/usr/local/go
export GOPATH=/opt/go/
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
# 定义目标操作系统和架构
targets=(
"darwin/amd64"
"darwin/arm64"
"linux/386"
"linux/amd64"
"linux/arm"
"linux/arm64"
"windows/386"
"windows/amd64"
)
# 获取Go文件名
go_file=$(ls *.go)
binary_name="${go_file%.*}"
# 编译目标文件
for target in "${targets[@]}"; do
os_name=${target%/*}
arch_name=${target#*/}
output_name="${binary_name}_${os_name}_${arch_name}"
if [ "$os_name" == "windows" ]; then
output_name="${output_name}.exe"
fi
if [ "$os_name" == "linux" ]; then
#env GOOS=$os_name GOARCH=$arch_name go build -ldflags "-linkmode external -extldflags '-static'" -o $output_name $go_file
env GOOS=$os_name GOARCH=$arch_name go build -o $output_name $go_file
else
env GOOS=$os_name GOARCH=$arch_name go build -o $output_name $go_file
fi
if [ $? -eq 0 ]; then
echo "Successfully built $output_name"
else
echo "Failed to build $output_name"
fi
done
go mod init changshang
go get -u
./build_all.sh
运行方式
cat xxx.log | changshang
tail -fn 40 xxx.log | changshang
echo 11:11:11:11:11:11 | changshang
下载
https://d2.sddts.cn/download/jingan/changshang