Class: Rbcli::RemoteExec

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcli/features/remote_exec.rb

Instance Method Summary collapse

Constructor Details

#initialize(cmd, connection_string, remote_identity, params, args, global_opts, config) ⇒ RemoteExec

Returns a new instance of RemoteExec.



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

	## SCRIPT COMMANDS
	if !@cmd.script.nil?
		@execution_block = lambda do |ssh|
			# Set paths
			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"

			# Upload scripts
			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}"

			# We need to test for JQ -- if it is not present we ask the user for the sudo password
			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

			# Since we need to sudo, we need to send the sudo password when prompted
			#result = ''
			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"
						# else
						# 	result += data.to_s
					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
			#puts result

			# Cleanup
			ssh.exec! "rm -rf #{tmpdir}"
		end

		## EXTERN COMMANDS
	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

		## STANDARD COMMANDS
	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

Instance Method Details

#runObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rbcli/features/remote_exec.rb', line 104

def run

	case @remote_host[:credstype]
	when :keyfile
		Net::SSH.start(@remote_host[:hostname], @remote_host[:username], port: @remote_host[:port], keys: @remote_host[:creds], keys_only: true, &@execution_block)
	when :keytext
		Net::SSH.start(@remote_host[:hostname], @remote_host[:username], port: @remote_host[:port], key_data: @remote_host[:creds], keys_only: true, &@execution_block)
	when :password
		Net::SSH.start(@remote_host[:hostname], @remote_host[:username], port: @remote_host[:port], password: @remote_host[:creds], keys_only: false, &@execution_block)
	else
		raise Exception.new "Invalid SSH Connection: No credentials specified for host #{@remote_host[:hostname]}"
	end

end