27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/rbcli/features/remote_exec.rb', line 27
def initialize cmd, connection_string, remote_identity, params, args, global_opts, config
@cmd = cmd
@params = params.to_json.gsub("\"", "\\\"")
@args = args.to_json.gsub("\"", "\\\"")
@global_opts = global_opts.to_json.gsub("\"", "\\\"")
@config = config.to_json.gsub("\"", "\\\"")
@remote_host = parse_connection connection_string, remote_identity
if !@cmd.script.nil?
@execution_block = lambda do |ssh|
tmpdir = ssh.exec! 'mktemp -d /tmp/rbcli.XXXXXXXXXXXX'
remote_script_path = "#{tmpdir.strip}/script.sh"
local_rbclilib_path = "#{File.dirname(__FILE__)}/../../../lib-sh/lib-rbcli.sh"
remote_rbclilib_path = "#{tmpdir.strip}/lib-rbcli.sh"
ssh.scp.upload @cmd.script.path, remote_script_path
ssh.scp.upload local_rbclilib_path, remote_rbclilib_path
ssh.exec! "chmod 0700 #{remote_script_path}"
jq_found = ssh.exec!('which jq').exitstatus == 0
if jq_found
sudopw = nil
else
print "JQ not found on remote system. Please enter sudo password to install it, or leave blank if no password needed: "
sudopw = gets.chomp
end
channel = ssh.open_channel do |channel, success|
channel.on_data do |channel, data|
print data.to_s
if !jq_found and data =~ /^\[sudo\] password for /
channel.send_data "#{sudopw}\n"
end
end
channel.request_pty
channel.exec "__RBCLI_PARAMS=\"#{@params}\" __RBCLI_ARGS=\"#{@args}\" __RBCLI_GLOBAL=\"#{@global_opts}\" __RBCLI_CONFIG=\"#{@config}\" source #{remote_script_path}"
channel.wait
end
channel.wait
ssh.exec! "rm -rf #{tmpdir}"
end
elsif !@cmd.extern.nil?
@execution_block = lambda do |ssh|
channel = ssh.open_channel do |channel, success|
channel.on_data do |channel, data|
print data.to_s
end
channel.request_pty
channel.exec "__RBCLI_PARAMS=\"#{@params}\" __RBCLI_ARGS=\"#{@args}\" __RBCLI_GLOBAL=\"#{@global_opts}\" __RBCLI_CONFIG=\"#{@config}\" __RBCLI_MYVARS=\"#{@myvars}\" #{@cmd.extern.path}"
channel.wait
end
channel.wait
end
else
@execution_block = lambda do |ssh|
raise Exception.new "Warning: Remote SSH Execution does not yetexist for Standard Ruby commands. Please use a Script or External command to use this feature."
end
end
end
|