mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
When sshing to transient VMs it's super useful to ignore host key checking. These options will make sure the host keys for the VMs are not being checked and that they are not getting added to your existing known hosts file.
44 lines
1.2 KiB
Ruby
44 lines
1.2 KiB
Ruby
class Ssh
|
|
|
|
def self.which(cmd)
|
|
# Gets path of executable for given command
|
|
|
|
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
|
|
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
|
exts.each { |ext|
|
|
exe = File.join(path, "#{cmd}#{ext}")
|
|
return exe if File.executable?(exe) && !File.directory?(exe)
|
|
}
|
|
end
|
|
return nil
|
|
end
|
|
|
|
def self.ssh(verbose, host_os, token, url)
|
|
ssh_path = which("ssh")
|
|
if !ssh_path
|
|
raise "Could not determine path to ssh"
|
|
end
|
|
os_types = {}
|
|
os_types[host_os] = 1
|
|
|
|
response = Pooler.retrieve(verbose, os_types, token, url)
|
|
if response["ok"] == true
|
|
if host_os =~ /win/
|
|
user = "Administrator"
|
|
else
|
|
user = "root"
|
|
end
|
|
|
|
hostname = "#{response[host_os]["hostname"]}.#{response["domain"]}"
|
|
ssh_options= ["-oStrictHostKeyChecking=no", "-oUserKnownHostsFile=/dev/null"]
|
|
cmd = "#{ssh_path} #{ssh_options.join(' ')} #{user}@#{hostname}"
|
|
|
|
# TODO: Should this respect more ssh settings? Can it be configured
|
|
# by users ssh config and does this respect those settings?
|
|
Kernel.exec(cmd)
|
|
else
|
|
raise "Could not get vm from vmpooler:\n #{response}"
|
|
end
|
|
return
|
|
end
|
|
end
|