class LinuxAdmin::SSHAgent
Attributes
pid[RW]
socket[R]
Public Class Methods
new(ssh_private_key, agent_socket = nil)
click to toggle source
# 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
Public Instance Methods
start()
click to toggle source
# 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()
click to toggle source
# 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() { |socket| ... }
click to toggle source
# File lib/linux_admin/ssh_agent.rb, line 27 def with_service start yield @socket ensure stop end
Private Instance Methods
parse_ssh_agent_output(output, index)
click to toggle source
# File lib/linux_admin/ssh_agent.rb, line 57 def parse_ssh_agent_output(output, index) output.split('=')[index].split(' ')[0].chop end
parse_ssh_agent_pid(output)
click to toggle source
# File lib/linux_admin/ssh_agent.rb, line 53 def parse_ssh_agent_pid(output) parse_ssh_agent_output(output, 2) end
parse_ssh_agent_socket(output)
click to toggle source
# File lib/linux_admin/ssh_agent.rb, line 49 def parse_ssh_agent_socket(output) parse_ssh_agent_output(output, 1) end
process_exists?(process_pid)
click to toggle source
# File lib/linux_admin/ssh_agent.rb, line 43 def process_exists?(process_pid) Process.kill(0, process_pid) == 1 rescue false end