Send command to jenkins agent and get output

Hello everyone.

I’m curious if there is an API call which i can issue so that i can send command to jenkins agent, agent will run the command on it’s host, then return reply/command output as http reply (json or plaintext)?

Currently, we can open up agent config from Jenkins, opet script console, type “println ‘uname -a’.execute()” and it will return them output in that page. But how about doing it via api call?

I would look at the following:

1 Like

Sure but i need to call that via API, not from script console. However you did push me in the right direction, running following call:

curl -H"Authorization: Basic <token> -d 'script=import hudson.util.RemotingDiagnostics;import jenkins.model.Jenkins;String agentName = "mytargetagent";String result;groovy_script = "println \"uname -a\".execute().text".trim();Jenkins.instance.slaves.find { agent -> == agentName; }.with { agent -> result = RemotingDiagnostics.executeGroovy(groovy_script,; }; println result;' https://jenkins.url/scriptText

Returns following:

Linux hostname 5.15.0-1019-aws #23~20.04.1-Ubuntu SMP Thu Aug 18 03:20:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Which is what i expected, however i wish there was possibly an easier way? Sure i can place the script in file that use @filename for script parameter but that just adds another layer of complexity.