/
removeHost.go
39 lines (33 loc) · 992 Bytes
/
removeHost.go
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
package nessusTools
import (
"fmt"
"os"
"github.com/beevik/etree"
)
//RemoveHosts removes a host and all associated issues from a nessus file.
func RemoveHosts(filePath string, hosts []string) (string, error) {
fmt.Println("Removing Hosts:")
for _, b := range hosts {
fmt.Println(" ", b)
}
//Open document and parse for issues
report := etree.NewDocument()
if err := report.ReadFromFile(filePath); err != nil {
return "", err
}
root := report.SelectElement("NessusClientData_v2")
for _, report := range root.SelectElements("Report") { //Select report branch to iterate over
for _, host := range report.SelectElements("ReportHost") { //Select Reporthost branch to iterate over
for _, b := range hosts {
if host.SelectAttrValue("name", "Not Found") == b {
report.RemoveChild(host)
}
}
}
}
//create a random filename and write to disk.
newFilePath := TempFileName()
report.WriteToFile(newFilePath)
os.Remove(filePath)
return newFilePath, nil
}