netexec shellCommand endpoint returns the error if shell command fails, instead of killing netexec

This commit is contained in:
Abhishek Shah 2015-09-21 16:32:51 -07:00
parent 915bc04488
commit e40f1533f9
2 changed files with 15 additions and 6 deletions

View File

@ -1,12 +1,10 @@
.PHONY: all netexec image push clean
TAG = 1.0
TAG = 1.1
PREFIX = gcr.io/google_containers
all: push
TAG = 1.0
netexec: netexec.go
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' ./netexec.go

View File

@ -191,9 +191,20 @@ func dialUDP(request string, remoteAddress *net.UDPAddr) (string, error) {
func shellHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.FormValue("shellCommand"))
output, err := exec.Command(shellPath, "-c", r.FormValue("shellCommand")).CombinedOutput()
assertNoError(err)
fmt.Fprintf(w, string(output))
cmdOut, err := exec.Command(shellPath, "-c", r.FormValue("shellCommand")).CombinedOutput()
output := map[string]string{}
if len(cmdOut) > 0 {
output["output"] = string(cmdOut)
}
if err != nil {
output["error"] = fmt.Sprintf("%v", err)
}
bytes, err := json.Marshal(output)
if err == nil {
fmt.Fprintf(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("response could not be serialized. %v", err), http.StatusExpectationFailed)
}
}
func hostNameHandler(w http.ResponseWriter, r *http.Request) {