Class: SSHBookmarker::Parser

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

Constant Summary collapse

DEFAULT_CONFIG_FILES =
['/etc/ssh/ssh_config', File.expand_path('~/.ssh/config')]
DEFAULT_KNOWN_HOSTS_FILES =
['/etc/ssh/known_hosts', File.expand_path('~/.ssh/known_hosts')]

Instance Method Summary collapse

Constructor Details

#initialize(locations_dir) ⇒ Parser

Returns a new instance of Parser.



8
9
10
# File 'lib/ssh_bookmarker.rb', line 8

def initialize(locations_dir)
  @locations_dir = locations_dir
end

Instance Method Details

#extract_url_scheme(scheme_comment) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/ssh_bookmarker.rb', line 115

def extract_url_scheme(scheme_comment)
  if scheme_comment && scheme_comment =~ /^#:(.*)$/
    $1.split(',')
  else
    ["ssh"]
  end
end

#loggerObject



16
17
18
19
20
# File 'lib/ssh_bookmarker.rb', line 16

def logger
  @logger ||= begin
                Logger.new(STDERR)
              end
end

#logger=(logger) ⇒ Object



12
13
14
# File 'lib/ssh_bookmarker.rb', line 12

def logger=(logger)
  @logger = logger
end

#make_webloc(hostname, port, url_scheme = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ssh_bookmarker.rb', line 93

def make_webloc(hostname, port, url_scheme=nil)
  url_scheme ||= 'ssh'
  logger.debug "Making host entry for #{url_scheme}://#{hostname}:#{port}"
  loc_filename = File.join(@locations_dir, "#{hostname} (#{url_scheme}).webloc")
  begin
    File.open(loc_filename, 'w') do |file|
      file.write <<-XML
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>URL</key>
        <string>#{url_scheme || 'ssh'}://#{hostname}#{port && ":#{port}"}</string>
    </dict>
    </plist>
  XML
    end
  rescue Exception => e
    logger.error "Can't write webloc file #{loc_filename} for host #{hostname}: #{e}"
  end
end

#parse_known_hosts_file(path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ssh_bookmarker.rb', line 61

def parse_known_hosts_file(path)
  File.open(path).each_line do |line|
    line = line.split(' ')[0]
    hostname = line.split(',').each do |hostname|
      if ported_host_match = hostname.match(/\[(\S*[a-zA-Z]+\S*)\]:([0-9]+)/)
        host = ported_host_match[1]
        port = ported_host_match[2]
        yield(host, port)
      else
        yield([hostname]) unless hostname.match(/ /) || hostname.match(/^[\[]/)  || hostname.match(/^[0-9\.]+$/) || hostname.match(/^[a-f0-9\:]+(%.*)?$/)
      end
    end
  end
rescue Errno::ENOENT => e
  puts "Can't open #{path}: #{e}" if $debug
end

#parse_ssh_config(path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ssh_bookmarker.rb', line 78

def parse_ssh_config(path)
  File.open(path).each do |line|
    if line.match /^\s*Host\s+([^#]+)\s*(#.+)?$/i
      host_spec = $1
      url_schemes = extract_url_scheme($2)
      hosts = host_spec.split(/\s+/)
      logger.debug("Got hosts #{hosts.inspect}")
      url_schemes.each do |url_scheme|
        yield(hosts, nil, url_scheme) unless hosts.any?{ |hn| hn.match(/\*/) }
      end
    end
  end
rescue Errno::ENOENT => e
end

#process_files(ssh_config_files = DEFAULT_CONFIG_FILES, known_host_files = DEFAULT_KNOWN_HOSTS_FILES) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ssh_bookmarker.rb', line 29

def process_files(ssh_config_files=DEFAULT_CONFIG_FILES, known_host_files=DEFAULT_KNOWN_HOSTS_FILES)
  ssh_config_files.each do |path|
    if File.exists?(path)
      logger.info("Parsing SSH config file #{path}")
    else
      logger.info("Skipping missing SSH config file #{path}")
      next
    end
    parse_ssh_config(path) do |hostnames, url_scheme|
      if @protocol_override
        (@protocol_override.call(hostnames, url_scheme) || [url_scheme]).each do |scheme|
          hostnames.each { |hostname| make_webloc(hostname, nil, scheme) }
        end
      else
        hostnames.each { |hostname| make_webloc(hostname, nil, url_scheme) }
      end
    end
  end

  known_host_files.each do |path|
    if File.exists?(path)
      logger.info("Parsing known_hosts file #{path}")
    else
      logger.info("Skipping missing known_hosts file #{path}")
      next
    end
    parse_known_hosts_file(path) do |hostname, port|
      make_webloc(hostname, port)
    end
  end
end

#protocol_override=(block) ⇒ Object



22
23
24
# File 'lib/ssh_bookmarker.rb', line 22

def protocol_override=(block)
  @protocol_override = block
end