-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (69 loc) · 1.59 KB
/
main.go
File metadata and controls
85 lines (69 loc) · 1.59 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"strings"
)
type IPMIRequest struct {
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password"`
Raw string `json:"raw"`
}
type IPMIResponse struct {
Output string `json:"output"`
Error string `json:"error"`
}
func handleIPMIRaw(w http.ResponseWriter, r *http.Request) {
var req IPMIRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
args := []string{
"-I", "lanplus",
"-H", req.Host,
"-U", req.Username,
"-P", req.Password,
"raw",
}
// Parse raw string into individual arguments
args = append(args, splitRawCommand(req.Raw)...)
fmt.Println(args)
cmd := exec.Command("ipmitool", args...)
out, err := cmd.CombinedOutput()
resp := IPMIResponse{
Output: string(out),
}
if err != nil {
resp.Error = err.Error()
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}
func splitRawCommand(raw string) []string {
return append([]string{}, splitAndClean(raw)...)
}
func splitAndClean(raw string) []string {
// You could sanitize or validate better here
return filterEmpty(splitBySpace(raw))
}
func splitBySpace(s string) []string {
return strings.Fields(s)
}
func filterEmpty(input []string) []string {
var out []string
for _, i := range input {
if i != "" {
out = append(out, i)
}
}
return out
}
func main() {
http.HandleFunc("/ipmi/raw", handleIPMIRaw)
fmt.Println("Starting server on :8080")
http.ListenAndServe(":8080", nil)
}