Return the output from exec as HTTP response

1. The problem I’m having:

I’m using a custom build with the http.handlers.exec module
My goal is to return the output from a command as HTTP Response
I don’t know how to do this or iif it’s possible at all with caddy

This is my current Config

:2015

route /update {
    exec powershell -command (get-date).toString() {
				log file caddy.log
    }
}

The Command output is logged to the logfile but the http response is {"status": "success"}

How to output the that is currently logged as http response?

2. Error messages and/or full log output:

caddy run --config Caddyfile
2024/02/05 11:33:57.291 INFO    using provided configuration    {"config_file": "Caddyfile", "config_adapter": ""}
2024/02/05 11:33:57.292 WARN    Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies    {"adapter": "caddyfile", "file": "Caddyfile", "line": 4}
2024/02/05 11:33:57.295 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["//127.0.0.1:2019", "//localhost:2019", "//[::1]:2019"]}
2024/02/05 11:33:57.295 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc0003f9a00"}
2024/02/05 11:33:57.295 INFO    http.log        server running  {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2024/02/05 11:33:57.296 INFO    autosaved config (load with --resume flag)      {"file": "C:\\Users\\....\\AppData\\Roaming\\Caddy\\autosave.json"}
2024/02/05 11:33:57.296 INFO    serving initial configuration
2024/02/05 11:33:57.298 WARN    tls     storage cleaning happened too recently; skipping for now        {"storage": "FileStorage:C:\\Users\\kragem\\AppData\\Roaming\\Caddy", "instance": "167e29d2-cc58-47ad-897b-1e8f482260d2", "try_again": "2024/02/06 11:33:57.298", "try_again_in": 86400}
2024/02/05 11:33:57.298 INFO    tls     finished cleaning storage units
2024/02/05 11:34:00.096 INFO    http.handlers.exec.exit         {"command": ["powershell", "-command", "(get-date).toString()"], "duration": 0.1574295}

3. Caddy version:

v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

b. Command:

caddy run --config Caddyfile

5. Links to relevant resources:

You likely want a CGI plugin like this one: GitHub - aksdb/caddy-cgi: Common Gateway Interface plugin for the Caddy HTTP server

But perhaps @abiosoft can answer your question better.

Currently, returning the output as part of the HTTP response is not supported.

IIRC, the main reason it was not implemented is to have a common approach for both background and foreground commands.

If this is indeed a common usecase for people, the feature can be added.

1 Like

Relevant open issue:

1 Like

Ah nice now i’m trying aksdb/caddy-cgi
That seems the right one.

But i’m running on an error while runnning on windows.
It does not matter in which format I enter the exe/script.

C:\Windows\system32\hostname.exe does exist

caddy run --config Caddyfile
2024/02/07 09:59:31.872 INFO    using provided configuration    {"config_file": "Caddyfile", "config_adapter": ""}
2024/02/07 09:59:31.876 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2024/02/07 09:59:31.876 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc000130d00"}
2024/02/07 09:59:31.876 INFO    http.log        server running  {"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2024/02/07 09:59:31.876 INFO    autosaved config (load with --resume flag)      {"file": "C:\\Users\\...\\AppData\\Roaming\\Caddy\\autosave.json"}
2024/02/07 09:59:31.877 INFO    serving initial configuration
2024/02/07 09:59:31.879 WARN    tls     storage cleaning happened too recently; skipping for now        {"storage": "FileStorage:C:\\Users\\...\\AppData\\Roaming\\Caddy", "instance": "0689bf7e-c42b-4533-9c97-07daf87cc2c0", "try_again": "2024/02/08 09:59:31.879", "try_again_in": 86400}
2024/02/07 09:59:31.879 INFO    tls     finished cleaning storage units
2024/02/07 09:59:33.781 INFO    CGI error: fork/exec .\hostname.exeC:\Windows\system32\hostname.exe: Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch.

Caddyfile

{
	order cgi before respond
}

:2015

cgi /update hostname.exe {
	#script_name /script.cgi
}

You should open an issue on that plugin’s repo.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.