2017-08-12 57 views
0

我需要關於這段代碼的一些建議。我在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 
} 

我真的很感謝你的幫助。

馬里奧。

回答

0

使用地圖[字符串]字符串(例如MAPNAME [計算機+文件名] = fileHashvalue,用於如果所述映射條目存在對於給定的計算機和文件名(串聯串中的每一行檢查 - COMPUTERNAME +文件名)&如果這樣比較與現有的哈希值),你可以在一個範圍循環而不是現在使用的兩個範圍內完成。希望這可以幫助。

-1

上testViolation你有(我刪除了谷歌瀏覽器進入更好的可見性):

line0 = 
c:\program files\internetexplorer\iexplore.exe; 
0f3c97716fed8b554a7ec0464d50719f; 
computer1 

line1: 
c:\windows\system32\notepad.exe; 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA; 
computer1 

line2: 
c:\program files\internetexplorer\iexplore.exe; 
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB; 
computer1 

line3: 
c:\windows\system32\notepad.exe; 
f60a9d3a9461f68de0fccebb0c6cb31a; 
computer1 

在你的循環:

for _, f := range lines { 
    splitLine = strings.Split(f, ";") 

    for _, c := range lines { 
    // To f == line0, you match with c == line2 
    // To f == line1, you match with c == line3 
    // To f == line2, you match with c == line0 
    // To f == line3, you match with c == line1 
     checkLine := strings.Split(c, ";") 
     if splitLine[0] == checkLine[0] && splitLine[2] == checkLine[2] && splitLine[1] != checkLine[1] { 
     // ... 
     } 
    } 
} 

所以,你需要刪除線,當你發現一個匹配。 它不是很性感,但它的工作

更簡單: 我認爲,你應該在begin函數上建立一個map [「string-untilFirst';'」] []「stringhash + computer」。

// map[string][]string 

所以,只要你數數元素:

for _, v := range myMap { 
    if len(v) > 1 { 
    // if you need compare computer you can split here or use: 
    // map[string]map[string][]string 
    // "there are a diff on v[0] and v[1] * len(v)" 
    } 
} 

我希望幫助你;) 很好的延續你的項目!