# File lib/rhc/ssh_helpers.rb, line 225 def exe?(executable) ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory| File.executable?(File.join(directory, executable.to_s)) end end
# File lib/rhc/ssh_helpers.rb, line 256 def fingerprint_for_default_key fingerprint_for_local_key(RHC::Config.ssh_pub_key_file_path) end
# File lib/rhc/ssh_helpers.rb, line 243 def fingerprint_for_local_key(key) Net::SSH::KeyFactory.load_public_key(key).fingerprint rescue NoMethodError, NotImplementedError => e ssh_keygen_fallback key nil rescue OpenSSL::PKey::PKeyError, Net::SSH::Exception => e error e.message nil rescue => e debug e.message nil end
Public: Generate an SSH key and store it in ~/.ssh/id_rsa
type - The String type RSA or DSS. bits - The Integer value for number of bits. comment - The String comment for the key
Examples
generate_ssh_key_ruby # => /home/user/.ssh/id_rsa.pub
Returns nil on failure or public key location as a String on success
# File lib/rhc/ssh_helpers.rb, line 200 def generate_ssh_key_ruby(type="RSA", bits = 2048, comment = "OpenShift-Key") key = RHC::Vendor::SSHKey.generate(:type => type, :bits => bits, :comment => comment) ssh_dir = RHC::Config.ssh_dir priv_key = RHC::Config.ssh_priv_key_file_path pub_key = RHC::Config.ssh_pub_key_file_path if File.exists?(priv_key) say "SSH key already exists: #{priv_key}. Reusing..." return nil else unless File.exists?(ssh_dir) FileUtils.mkdir_p(ssh_dir) File.chmod(0700, ssh_dir) end File.open(priv_key, 'w') {|f| f.write(key.private_key)} File.chmod(0600, priv_key) File.open(pub_key, 'w') {|f| f.write(key.ssh_public_key)} ssh_add end pub_key end
return whether or not SSH is installed
# File lib/rhc/ssh_helpers.rb, line 283 def has_ssh? @has_ssh ||= begin @ssh_version = nil ssh_version $?.success? rescue false end end
# File lib/rhc/ssh_helpers.rb, line 134 def run_on_gears(command, gears, opts={}, &block) debug "Executing #{command} on each of #{gears.inspect}" MultipleGearTask.new(command, gears, {:limit => options.limit, :always_prefix => options.always_prefix, :raw => options.raw}.merge(opts)).run(&block) end
# File lib/rhc/ssh_helpers.rb, line 145 def ssh_command_for_op(operation) #case operation raise RHC::OperationNotSupportedException, "The operation #{operation} is not supported." #end end
for an SSH public key specified by 'key', return a triple
which is basically the space-separated list of the SSH public key content
# File lib/rhc/ssh_helpers.rb, line 263 def ssh_key_triple_for(key) begin IO.read(key).chomp.split rescue Errno::ENOENT => e raise ::RHC::KeyFileNotExistentException.new("File '#{key}' does not exist.") rescue Errno::EACCES => e raise ::RHC::KeyFileAccessDeniedException.new("Access denied to '#{key}'.") end end
# File lib/rhc/ssh_helpers.rb, line 273 def ssh_key_triple_for_default_key ssh_key_triple_for(RHC::Config.ssh_pub_key_file_path) end
For Net::SSH versions (< 2.0.11) that does not have Net::SSH::KeyFactory.load_public_key, we drop to shell to get the key's fingerprint
# File lib/rhc/ssh_helpers.rb, line 234 def ssh_keygen_fallback(path) fingerprint = %xssh-keygen -lf #{path} 2>&1`.split(' ')[1] if $?.exitstatus != 0 error "Unable to compute SSH public key finger print for #{path}" end fingerprint end
Public: Run ssh command on remote host
host - The String of the remote hostname to ssh to. username - The String username of the remote user to ssh as. command - The String command to run on the remote host.
Examples
ssh_ruby('myapp-t.rhcloud.com', '109745632b514e9590aa802ec015b074', 'rhcsh tail -f $OPENSHIFT_LOG_DIR/*"') # => true
Returns true on success
# File lib/rhc/ssh_helpers.rb, line 165 def ssh_ruby(host, username, command) debug "Opening Net::SSH connection to #{host}, #{username}, #{command}" Net::SSH.start(host, username) do |session| #:nocov: session.open_channel do |channel| channel.request_pty do |ch, success| say "pty could not be obtained" unless success end channel.on_data do |ch, data| puts data end channel.exec command end session.loop #:nocov: end rescue Errno::ECONNREFUSED => e raise RHC::SSHConnectionRefused.new(host, username) rescue SocketError => e raise RHC::ConnectionFailed, "The connection to #{host} failed: #{e.message}" end
check the version of SSH that is installed
# File lib/rhc/ssh_helpers.rb, line 278 def ssh_version @ssh_version ||= %xssh -V 2>&1`.strip end
# File lib/rhc/ssh_helpers.rb, line 139 def table_from_gears(command, groups, opts={}, &block) cells = run_on_gears(command, groups, {:as => :table}.merge(opts), &block) cells.each{ |r| r.concat(r.pop.first.split(opts[:split_cells_on])) } if !block_given? && opts[:split_cells_on] say table cells, opts unless options.raw end
# File lib/rhc/ssh_helpers.rb, line 295 def ssh_add if exe?('ssh-add') #:nocov: %xssh-add 2&>1` #:nocov: end end