59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
// ... (Hostname handling as before) ...
|
|
|
|
failOnError := os.Getenv("FAIL_ON_ERROR") == "true"
|
|
commandStr := os.Getenv("COMMAND")
|
|
errorCommandStr := os.Getenv("ERROR_COMMAND") // Fetch error command
|
|
|
|
for _, host := range hosts {
|
|
// ... (DNS Resolution as before) ...
|
|
|
|
// Ping
|
|
pingCmd := exec.Command("ping", "-c", "3", host)
|
|
pingOutput, err := pingCmd.Output()
|
|
if err != nil {
|
|
fmt.Println("Ping Failed:", err)
|
|
if failOnError {
|
|
os.Exit(1) // Exit with error code
|
|
}
|
|
|
|
// Execute error command if specified
|
|
if errorCommandStr != "" {
|
|
cmdParts := strings.Fields(errorCommandStr)
|
|
cmd := exec.Command(cmdParts[0], cmdParts[1:]...)
|
|
cmdOutput, cmdErr := cmd.Output()
|
|
if cmdErr != nil {
|
|
fmt.Println("Error Command Execution Failed:", errorCommandStr, cmdErr)
|
|
} else {
|
|
fmt.Println("Error Command Output:\n", string(cmdOutput))
|
|
}
|
|
}
|
|
} else {
|
|
fmt.Println("Ping Output:\n", string(pingOutput))
|
|
}
|
|
|
|
// Execute additional command if specified
|
|
if commandStr != "" {
|
|
cmdParts := strings.Fields(commandStr) // Split on spaces
|
|
cmd := exec.Command(cmdParts[0], cmdParts[1:]...)
|
|
cmdOutput, err := cmd.Output()
|
|
if err != nil {
|
|
fmt.Println("Command Execution Failed:", cmdStr, err)
|
|
} else {
|
|
fmt.Println("Command Output:\n", string(cmdOutput))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|