mirror of
https://github.com/byReqz/probehost2.git
synced 2024-11-21 22:33:14 +00:00
add mtr func
This commit is contained in:
parent
051671036c
commit
ac8aac1f60
24
main.go
24
main.go
@ -28,6 +28,10 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runner(remoteip string, command string, args... string) string{
|
func runner(remoteip string, command string, args... string) string{
|
||||||
|
logfile.WithFields(log.Fields{
|
||||||
|
"remote_ip": remoteip,
|
||||||
|
"command": fmt.Sprint(command, args),
|
||||||
|
}).Info("request initiated:")
|
||||||
cmd, err := exec.Command(command, args...).Output()
|
cmd, err := exec.Command(command, args...).Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1
|
if ! strings.Contains(err.Error(), "1") { // dont exit if error code is 1
|
||||||
@ -35,7 +39,7 @@ func runner(remoteip string, command string, args... string) string{
|
|||||||
"remote_ip": remoteip,
|
"remote_ip": remoteip,
|
||||||
"command": fmt.Sprint(command, args),
|
"command": fmt.Sprint(command, args),
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
}).Warn("the following request failed:")
|
}).Warn("request failed:")
|
||||||
logfile.WithFields(log.Fields{
|
logfile.WithFields(log.Fields{
|
||||||
"remote_ip": remoteip,
|
"remote_ip": remoteip,
|
||||||
"command": fmt.Sprint(command, args),
|
"command": fmt.Sprint(command, args),
|
||||||
@ -58,16 +62,28 @@ func showhelp(w http.ResponseWriter, req *http.Request) {
|
|||||||
func ping(w http.ResponseWriter, req *http.Request) {
|
func ping(w http.ResponseWriter, req *http.Request) {
|
||||||
geturl := strings.Split(req.URL.String(), "/")
|
geturl := strings.Split(req.URL.String(), "/")
|
||||||
target := geturl[2]
|
target := geturl[2]
|
||||||
pingres := runner(req.RemoteAddr, "ping", "-c5", target)
|
res := runner(req.RemoteAddr, "ping", "-c10", target)
|
||||||
if pingres == "" {
|
if res == "" {
|
||||||
fmt.Fprintln(w, http.StatusInternalServerError)
|
fmt.Fprintln(w, http.StatusInternalServerError)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(w, pingres)
|
fmt.Fprint(w, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mtr(w http.ResponseWriter, req *http.Request) {
|
||||||
|
geturl := strings.Split(req.URL.String(), "/")
|
||||||
|
target := geturl[2]
|
||||||
|
res := runner(req.RemoteAddr, "mtr", "-c10", "-w", target)
|
||||||
|
if res == "" {
|
||||||
|
fmt.Fprintln(w, http.StatusInternalServerError)
|
||||||
|
} else {
|
||||||
|
fmt.Fprint(w, res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/ping/", ping)
|
http.HandleFunc("/ping/", ping)
|
||||||
|
http.HandleFunc("/mtr/", mtr)
|
||||||
http.HandleFunc("/", showhelp)
|
http.HandleFunc("/", showhelp)
|
||||||
fmt.Println("Serving on :8000")
|
fmt.Println("Serving on :8000")
|
||||||
http.ListenAndServe(":8000", nil)
|
http.ListenAndServe(":8000", nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user