Class: MJ::Tools::SSH

Inherits:
Object
  • Object
show all
Includes:
SubProcess
Defined in:
lib/mj/tools/ssh.rb

Class Method Summary collapse

Class Method Details

.add_key(key_file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/mj/tools/ssh.rb', line 21

def self.add_key( key_file )
    if @keys_added.index( key_file )
        return true
    end
    if ssh_add( "#{key_file}" ) == 0
        @keys_added << key_file
        return true
    end
    return false
end

.cleanupObject



32
33
34
35
36
37
# File 'lib/mj/tools/ssh.rb', line 32

def self.cleanup
    @keys_added.each do |file|
        self.remove_key( file )
    end
    @keys_added = []
end

.has_key?(key_file) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mj/tools/ssh.rb', line 39

def self.has_key?( key_file )
    if @keys_added.index( key_file )
        return true
    end
    found = false
    ssh_add '-l' do
        |line|
        if line.index(key_file)
            return found = true
        end
    end
    return found
end

.has_keys?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/mj/tools/ssh.rb', line 13

def self.has_keys?
    return ! @keys_added.empty?
end

.keysObject



17
18
19
# File 'lib/mj/tools/ssh.rb', line 17

def self.keys
    return @keys_added
end

.list_keysObject



61
62
63
# File 'lib/mj/tools/ssh.rb', line 61

def self.list_keys
    ssh_add( "-l" )
end

.remove_all_keysObject



57
58
59
# File 'lib/mj/tools/ssh.rb', line 57

def self.remove_all_keys
    ssh_add( "-D" )
end

.remove_key(key_file) ⇒ Object



53
54
55
# File 'lib/mj/tools/ssh.rb', line 53

def self.remove_key( key_file )
    ssh_add( "-d #{key_file}" )
end

.ssh_add(command, &block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/mj/tools/ssh.rb', line 65

def self.ssh_add( command,  &block )
    if block_given?
        self.execute "ssh-add #{command}", &block
    else
        self.execute "ssh-add #{command}"
    end
end