Class: LinuxAdmin::SSHAgent
- Inherits:
-
Object
- Object
- LinuxAdmin::SSHAgent
- Defined in:
- lib/linux_admin/ssh_agent.rb
Instance Attribute Summary collapse
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
-
#initialize(ssh_private_key, agent_socket = nil) ⇒ SSHAgent
constructor
A new instance of SSHAgent.
- #start ⇒ Object
- #stop ⇒ Object
- #with_service ⇒ Object
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
#pid ⇒ Object
Returns the value of attribute pid.
4 5 6 |
# File 'lib/linux_admin/ssh_agent.rb', line 4 def pid @pid end |
#socket ⇒ Object (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
#start ⇒ Object
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 |
#stop ⇒ Object
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 2>&1') if process_exists?(@pid) File.delete(@socket) if File.exist?(@socket) @socket = nil @pid = nil end |
#with_service ⇒ Object
27 28 29 30 31 32 |
# File 'lib/linux_admin/ssh_agent.rb', line 27 def with_service start yield @socket ensure stop end |