@@ -21,6 +21,7 @@ A tool that allows you to convert NMAP XML output to html/csv/json/markdown.
2121 - [Download Binary](#download-binary)
2222 - [Compile](#compile)
2323 - [ Example] ( #example )
24+ - [ Use as a library] ( #use-as-a-library )
2425
2526## Usage
2627
@@ -132,7 +133,7 @@ docker run -v /path/to/xml/file.xml:/opt/file.xml ghcr.io/vdjagilev/nmap-formatt
132133Choose version from Release page and download it:
133134
134135```
135- curl https://github.com/vdjagilev/nmap-formatter/releases/download/v0.3.0 /nmap-formatter-linux-amd64.tar.gz --output nmap-formatter.tar.gz -L
136+ curl https://github.com/vdjagilev/nmap-formatter/releases/download/v0.3.1 /nmap-formatter-linux-amd64.tar.gz --output nmap-formatter.tar.gz -L
136137tar -xzvf nmap-formatter.tar.gz
137138./nmap-formatter --help
138139```
@@ -157,3 +158,63 @@ nmap-formatter basic-example.xml html
157158```
158159
159160![ Basic HTML Example] ( docs/images/basic-example-html.png )
161+
162+ ## Use as a library
163+
164+ How to parse nmap results using golang
165+
166+ ``` go
167+ package main
168+
169+ import (
170+ " encoding/xml"
171+ " os"
172+
173+ " github.com/vdjagilev/nmap-formatter/formatter"
174+ )
175+
176+ func main () {
177+ var nmap formatter.NMAPRun
178+ var config formatter.Config = formatter.Config {}
179+
180+ // Read XML file that was produced by nmap (with -oX option)
181+ content , err := os.ReadFile (" example.xml" )
182+ if err != nil {
183+ panic (err)
184+ }
185+ // Unmarshal XML and map structure(s) fields accordingly
186+ if err = xml.Unmarshal (content, &nmap); err != nil {
187+ panic (err)
188+ }
189+
190+ // Output data to console stdout
191+ // You can use any other io.Writer implementation
192+ // for example: os.OpenFile("file.json", os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
193+ config.Writer = os.Stdout
194+ // Formatting data to JSON, you can use:
195+ // CSVOutput, MarkdownOutput, HTMLOutput as well
196+ config.OutputFormat = formatter.JSONOutput
197+
198+ // Setting formatter data/options
199+ templateData := formatter.TemplateData {
200+ NMAPRun: nmap, // NMAP output data itself
201+ OutputOptions: formatter.OutputOptions {
202+ JSONPrettyPrint: true , // Additional option to prettify JSON
203+ },
204+ }
205+
206+ // New formatter instance
207+ formatter := formatter.New (&config)
208+ if formatter == nil {
209+ // Not json/markdown/html/csv
210+ panic (" wrong formatter provided" )
211+ }
212+
213+ // Attempt to format the data
214+ if err = formatter.Format (&templateData); err != nil {
215+ // html template could not be parsed or some other issue occured
216+ panic (err)
217+ }
218+ }
219+
220+ ```
0 commit comments