我需要關於這段代碼的一些建議。我在Go中開發一個應用程序,逐行讀取文件的內容,並將由散列形成的特定列與同一文件中的其他列進行比較。我試圖捕捉不同的哈希到相同的文件名,並得到一個文件已被更改的警報。比較文本文件的列
下面是一個文本文件的例子。每一行按文件名,哈希和計算機名稱構成:
c:\program files\internet explorer\iexplore.exe;0f3c97716fed8b554a7ec0464d50719f;computer1
c:\program files (x86)\google\chrome\application\chrome.exe;d387a06cd4bf5fcc1b50c3882f41a44e;computer1
c:\windows\system32\notepad.exe;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;computer1
c:\program files\internet explorer\iexplore.exe;BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB;computer1
c:\program files (x86)\google\chrome\application\chrome.exe;d387a06cd4bf5fcc1b50c3882f41a44e;computer1
c:\windows\system32\notepad.exe;f60a9d3a9461f68de0fccebb0c6cb31a;computer1
運行時我得到了相同的信息兩次,因爲我用了兩個的代碼。很明顯,我只需要每個事件發生一次。結果:
go run main.go
Original: c:\program files\internet explorer\iexplore.exe 0f3c97716fed8b554a7ec0464d50719f computer1
Violation: c:\program files\internet explorer\iexplore.exe BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB computer1
------
Original: c:\windows\system32\notepad.exe AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA computer1
Violation: c:\windows\system32\notepad.exe f60a9d3a9461f68de0fccebb0c6cb31a computer1
------
Original: c:\program files\internet explorer\iexplore.exe BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB computer1
Violation: c:\program files\internet explorer\iexplore.exe 0f3c97716fed8b554a7ec0464d50719f computer1
------
Original: c:\windows\system32\notepad.exe f60a9d3a9461f68de0fccebb0c6cb31a computer1
Violation: c:\windows\system32\notepad.exe AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA computer1
------
IGNORE THIS LINE []
IGNORE THIS LINE []
預期結果:
c:\program files\internet explorer\iexplore.exe;0f3c97716fed8b554a7ec0464d50719f;computer1
c:\program files\internet explorer\iexplore.exe;BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB;computer1
c:\windows\system32\notepad.exe;AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;computer1
c:\windows\system32\notepad.exe;f60a9d3a9461f68de0fccebb0c6cb31a;computer1
我想休息一下,繼續,變量等,但我認爲,我在與邏輯故障報告僅出現一次。
下面的代碼(最後):
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
const (
FILE_DIR = "./logs/"
)
func main() {
p := fmt.Println
// List and open all log files
listOfFiles := listDirFiles(FILE_DIR)
for _, file := range listOfFiles {
f, err := os.Open(FILE_DIR + file)
if err != nil {
p("Error when opening the file " + FILE_DIR + file + " .")
break
}
defer f.Close()
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text()) // Converting the content of a file into a slice
}
results := testViolation(lines)
fmt.Println("IGNORE THIS LINE", results)
}
}
func testViolation(lines []string) []string {
var splitLine []string
for _, f := range lines {
splitLine = strings.Split(f, ";")
for _, c := range lines {
checkLine := strings.Split(c, ";")
if splitLine[0] == checkLine[0] && splitLine[2] == checkLine[2] && splitLine[1] != checkLine[1] {
fmt.Println("Original: ", splitLine[0], splitLine[1], splitLine[2])
fmt.Println("Violation: ", checkLine[0], checkLine[1], checkLine[2])
fmt.Println("------")
}
}
}
return nil
}
func listDirFiles(dir string) []string {
var filesString []string
files, _ := ioutil.ReadDir(dir)
// Convert []os.FileInfo into []string to return
for _, f := range files {
filesString = append(filesString, f.Name())
}
return filesString
}
我真的很感謝你的幫助。
馬里奧。