Skip to content

Instantly share code, notes, and snippets.

/capture_ssh.rb Secret

Created January 29, 2016 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5833cc8d4d0e58354590 to your computer and use it in GitHub Desktop.
Save anonymous/5833cc8d4d0e58354590 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pry'
servers = {
"MAIN_SERVER_1" => "192.168.1.1" # <<-- SERVER IP HERE
}
user = 'user'
command = "tail -F /path/to/log.log | grep 'text here'"
thrs = []
servers.each do |server, ip|
thrs << Thread.new do
puts "Starting Monitor for #{server} @ #{ip}"
call = "ssh #{user}@#{ip} #{command}"
puts "Making following ssh call: #{call}"
# This seems to output to STDOUT
# Comment this out to test the IO capture block below
system(call)
# My attempt to wrap this call in a popen call and handle the IO stream with select
IO.popen(call) do |stream|
while true
r, w, e = IO.select([stream], [], [], 1)
puts "Select fired / timed out"
# I would expect r (the readable IO array) to have data since stdout has data?
# But it always ends up returning nil
p r
unless r.nil?
puts r.first.gets
end
# If you enable this line, it will drop into the pry shell
# Oddly, keystrokes seem to be interruped while in this mode...
# But more interestingly, if I perform a stream.gets I see the line return...?
# binding.pry
end
end
end
end
thrs.each { |thr| thr.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment