Module: DTK::Client::SSHUtil
- Defined in:
- lib/util/ssh_util.rb
Class Method Summary collapse
- .default_rsa_pub_key_path ⇒ Object
- .read_and_validate_pub_key(path_to_pub_key) ⇒ Object
- .rsa_pub_key_content ⇒ Object
- .ssh_reachable?(user, server, timeout = 3) ⇒ Boolean
- .update_ssh_known_hosts(server_dns, server_fingerprint) ⇒ Object
Class Method Details
.default_rsa_pub_key_path ⇒ Object
43 44 45 |
# File 'lib/util/ssh_util.rb', line 43 def self.default_rsa_pub_key_path() "#{ssh_base_dir()}/id_rsa.pub" end |
.read_and_validate_pub_key(path_to_pub_key) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/util/ssh_util.rb', line 22 def self.read_and_validate_pub_key(path_to_pub_key) is_ssh_key_path_valid?(path_to_pub_key) rsa_pub_key = File.open(path_to_pub_key) { |f| f.read } is_ssh_key_content_valid?(rsa_pub_key) rsa_pub_key end |
.rsa_pub_key_content ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/util/ssh_util.rb', line 47 def self.rsa_pub_key_content() path_to_key = self.default_rsa_pub_key_path() unless File.file?(path_to_key) raise DtkError,"No File found at (#{path_to_key}). Path is wrong or it is necessary to generate the public rsa key (e.g., run ssh-keygen -t rsa)" end content = File.open(path_to_key){ |f| f.read } content.chomp end |
.ssh_reachable?(user, server, timeout = 3) ⇒ Boolean
57 58 59 60 61 62 |
# File 'lib/util/ssh_util.rb', line 57 def self.ssh_reachable?(user, server, timeout = 3) output = `ssh -o ConnectTimeout=#{timeout} -o \"StrictHostKeyChecking no\" -o \"UserKnownHostsFile /dev/null\" -q #{user}@#{server} exit; echo $?` # if response 0 than it is able to connect "0".eql?(output.strip()) end |
.update_ssh_known_hosts(server_dns, server_fingerprint) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/util/ssh_util.rb', line 29 def self.update_ssh_known_hosts(server_dns,server_fingerprint) known_hosts_path = ssh_known_hosts_path() if File.file?(known_hosts_path) `ssh-keygen -f #{known_hosts_path} -R #{server_dns} 2> /dev/null` File.open(known_hosts_path,"a"){|f|f << server_fingerprint} else ssh_base_dir = ssh_base_dir() unless File.directory?(ssh_base_dir) Dir.mkdir(ssh_base_dir) end File.open(known_hosts_path,"w"){|f|f << server_fingerprint} end end |