Class: LinuxAdmin::SSHAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/linux_admin/ssh_agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssh_private_key, agent_socket = nil) ⇒ SSHAgent

Returns a new instance of SSHAgent.



7
8
9
10
# File 'lib/linux_admin/ssh_agent.rb', line 7

def initialize(ssh_private_key, agent_socket = nil)
  @socket = agent_socket
  @private_key = ssh_private_key
end

Instance Attribute Details

#pidObject

Returns the value of attribute pid.



4
5
6
# File 'lib/linux_admin/ssh_agent.rb', line 4

def pid
  @pid
end

#socketObject (readonly)

Returns the value of attribute socket.



5
6
7
# File 'lib/linux_admin/ssh_agent.rb', line 5

def socket
  @socket
end

Instance Method Details

#startObject

Raises:

  • (StandardError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/linux_admin/ssh_agent.rb', line 12

def start
  if @socket
    FileUtils.mkdir_p(File.dirname(@socket))
    agent_details = `ssh-agent -a #{@socket}`
  else
    agent_details = `ssh-agent`
    @socket = parse_ssh_agent_socket(agent_details)
  end
  @pid = parse_ssh_agent_pid(agent_details)
  IO.popen({'SSH_AUTH_SOCK' => @socket, 'SSH_AGENT_PID' => @pid}, ['ssh-add', '-'], :mode => 'w') do |f|
    f.puts(@private_key)
  end
  raise StandardError, "Couldn't add key to agent" if $CHILD_STATUS.to_i != 0
end

#stopObject



34
35
36
37
38
39
# File 'lib/linux_admin/ssh_agent.rb', line 34

def stop
  system({'SSH_AGENT_PID' => @pid}, '(ssh-agent -k) &> /dev/null') if process_exists?(@pid)
  File.delete(@socket) if File.exist?(@socket)
  @socket = nil
  @pid = nil
end

#with_serviceObject



27
28
29
30
31
32
# File 'lib/linux_admin/ssh_agent.rb', line 27

def with_service
  start
  yield @socket
ensure
  stop
end