hi Dave,

On Thu, Feb 15, 2018 at 3:56 AM, David Brendlinger <
davidbrendling...@gmail.com> wrote:

> can someone create an example for me of a webpage that can run a bash
> command ?
>
> I would like a index.html that has a button on it.  you hit the button and
> it would do a     ls /tmp >> myfile
>

here is something you can start playing with:

```
package main

import (
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"strings"
)

var (
addrFlag = flag.String("addr", ":5555", "server address:port")
)

func main() {
flag.Parse()
http.HandleFunc("/", helloWorld)
err := http.ListenAndServe(*addrFlag, nil)
if err != nil {
log.Fatal(err)
}
}

func helloWorld(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("ls", "/tmp")
out, err := cmd.Output()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, ">>> %s\n", strings.Join(cmd.Args, " "))
fmt.Fprintf(w, "%s\n", string(out))
}

```

this will run the "ls" command on the server, not the client, though :)

and I left the button part as an exercize for the reader :P

I am a newby :)
>
welcome!

-s

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to