forked from jofu/shellcmd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellcmd.rb
34 lines (27 loc) · 1.01 KB
/
shellcmd.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module MCollective
module Agent
class Shellcmd<RPC::Agent
metadata :name => "Run shell commands, get output",
:description => "Run arbitrary shell commands and get their output.",
:author => "Joe Miller, updates by Joe McDonagh",
:license => "GPLv2",
:version => "1.1",
:url => "http://github.com/joemiller/shellcmd-agent",
:timeout => 300
action "runcmd" do
validate :cmd, String
require 'open4'
# Set empty strings for stdout and stderr reply so << will dtrt
reply[:stdout] = ""
reply[:stderr] = ""
exitcode = Open4.popen4("#{request[:cmd]}") do |pid, stdin, stdout, stderr|
# Just in case we execute a program that waits for stdin by accident
stdin.reopen(File::open('/dev/null', 'r'))
reply[:stdout] << stdout.gets until stdout.eof?
reply[:stderr] << stderr.gets until stderr.eof?
end
reply[:exitcode] = exitcode
end
end
end
end