Skip to content

Instantly share code, notes, and snippets.

@andrewn
Created February 19, 2017 14:40
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 andrewn/7b7cf84978ebec6e641fc6fec4914db2 to your computer and use it in GitHub Desktop.
Save andrewn/7b7cf84978ebec6e641fc6fec4914db2 to your computer and use it in GitHub Desktop.
Output hosts advertising ssh via Bonjour/MDNS
#!/usr/bin/env ruby
def host_name(line='')
matches = /_ssh._tcp.\s*(.*)$/.match(line)
matches[1] unless matches.nil? || matches[1].nil?# || !/ /.match(matches[1]).nil?
end
TIMEOUT_SEC = 1
read_results = Thread.new do
Thread.current[:lines] = []
discovery = IO.popen('dns-sd -B _ssh._tcp .')
while line = discovery.readline do
Thread.current[:lines] << host_name(line)
end
end
# timer
timer = Thread.new { sleep TIMEOUT_SEC; read_results.kill; puts }
read_results.join
results = read_results[:lines].compact.map { |r| "#{r.gsub(' ', '\ ')}.local" }.uniq
puts results.join(" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment