func handleClient(conn net.Conn, config_obj *read_config.ReadConfig) { // close connection on exit defer conn.Close() pkt_rcv, _ := common.ReceivePacket(conn) cmd := string(pkt_rcv.CommandBuffer[:common.GetLen(pkt_rcv.CommandBuffer[:])]) pkt_rcv_crc32value := pkt_rcv.CRC32Value if crc32, _ := common.DoCRC32(&pkt_rcv); crc32 != pkt_rcv_crc32value { fmt.Println("WARNING: CRC not matching", crc32, pkt_rcv_crc32value) } pkt_send := common.PrepareToSend(cmd, common.RESPONSE_PACKET) if pkt_send.ResultCode == common.STATE_UNKNOWN { //its a response, but not to the HELLO_COMMAND if config_obj.IsCommandAllowed(cmd) { str_cmd := config_obj.GetCommand(cmd) fmt.Println("executing:", str_cmd) return_id, return_stdout := common.ExecuteCommand(str_cmd) pkt_send.ResultCode = return_id copy(pkt_send.CommandBuffer[:], return_stdout) pkt_send.CRC32Value, _ = common.DoCRC32(&pkt_send) } else { pkt_send.ResultCode = common.STATE_CRITICAL } } err := common.SendPacket(conn, pkt_send) common.CheckError(err) }
func main() { if len(os.Args) < 2 { fmt.Printf("%s -h for help\n", os.Args[0]) os.Exit(1) } var host = goopt.String([]string{"-H", "--host"}, "127.0.0.1", "The remote host running NRPE-Server") var port = goopt.Int([]string{"-p", "--port"}, 5666, "The remote port on which the NRPE-server listens") var transport = goopt.Int([]string{"-t", "--transport"}, 0, "Transport type: 0 - clear, 1 - ssl, 2 -ssh") var command = goopt.String([]string{"-c", "--command"}, "version", "The check command defined in the nrpe.cfg file you would like to trigger") goopt.Parse(nil) service := fmt.Sprintf("%s:%d", *host, *port) conn := prepareConnection(service, *transport) pkt_to_send := common.PrepareToSend(*command, common.QUERY_PACKET) err := common.SendPacket(conn, pkt_to_send) common.CheckError(err) response_from_command, _ := common.ReceivePacket(conn) fmt.Println(string(response_from_command.CommandBuffer[:])) os.Exit(int(response_from_command.ResultCode)) }