Hi,
I am writing a webserver in go that will render a webpage with a button and a textarea. On the click of the button go will execute a terminal command and display its output per line in a
tag I defined in my html. I am using gorilla websocket to do this. The command I run outputs an xml file, which I parse in go, and I want to output this data in the textarea on my webpage after the command execution has finished, but nothing is being rendered to the textarea.
Here is a snippet of my code:
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
}
nmap_chain := "nmap -F -sS 172.16.2.0/24 -oX output.xml --stats-every 2s | grep --line-buffered 'Ping Scan Timing' | mawk -W interactive '{print $5}'"
cmd := exec.Command("bash","-c",nmap_chain)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
in := bufio.NewScanner(stdout)
for in.Scan() {
c := []byte(in.Text())
err = conn.WriteMessage(websocket.TextMessage, c)
if err != nil {
fmt.Println(err)
}
time.Sleep(time.Second)
}
if err := cmd.Wait(); err == nil {
p := Test{}
p.SetName("hello")
} else {
log.Fatal(err)
}
conn.Close()
}
func handler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("index.html")
t.Execute(w,p)
}
func main() {
http.HandleFunc("/", handler)
http.Handle("/layout/", http.StripPrefix("/layout/", http.FileServer(http.Dir("layout"))))
http.HandleFunc("/exec", execCommandHandler)
http.HandleFunc("/websocket", wsHandler)
http.ListenAndServe(":8080", nil)
}
And here is my html file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>NMAP Demo</title>
<link rel="stylesheet" type="text/css" href="layout/mystyle.css">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
function myWebsocketStart() {
var ws = new WebSocket("ws://localhost:8080/websocket");
ws.onmessage = function(evt) {
var myTextArea = document.getElementById("textarea1");
<!-- myTextArea.value = myTextArea.value + "\n" + evt.data --> document.getElementById("textarea1").innerHTML = evt.data $('#exec').prop('disabled', true);
};
ws.onclose = function() {
$('#exec').prop('disabled', false)
};
}
</script>
</head>
<body>
<div id="headerBar"></div>
<div id="buttonformat">
<button onclick="javascript:myWebsocketStart()" id="exec">Execute</button>
</div>
<div id="results">
<textarea readonly id="response" rows="50" cols="50" style="resize">{{.Value}}</textarea>
</div>
<div>
<p id="textarea1"></p>
</div>
</body>
</html>
As stated above, my issue right now is that when I render my webpage, and press the button, it starts streaming the output process of the terminal command, but after the execution is finished, my textarea is not populated with anything. Right now I am just trying to print a simple hello string to my textarea as shown above in the code snippet after my command execution has finished but nothing gets outputted to it.