Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Created October 30, 2019 22:30
Show Gist options
  • Save ta1kt0me/4939aaeedbcefe5c04c5f4eb12441fff to your computer and use it in GitHub Desktop.
Save ta1kt0me/4939aaeedbcefe5c04c5f4eb12441fff to your computer and use it in GitHub Desktop.
converts ssh_config to essh config.lua
ssh_config = File.read(ENV.fetch('HOME') + "/.ssh/config")
group = {}
host = []
ssh_config.each_line do |line|
line.strip!
if line.start_with?(/Host |host /)
unless host.empty?
values = host[1..-1].map { |str| [str.strip.split(" ", 2)].to_h }.inject(&:merge)
group[host[0]] = values
end
host = [line.split(" ", 2).last]
elsif line.match?(/\A\z/)
next
else
host << line
end
end
config = group.map do |host_name, options|
option_contents = options.transform_keys(&:capitalize).map do |key, value|
<<~TEMPLATE
#{key} = "#{value}",
TEMPLATE
end
option_contents << [
"description = \"\",\n",
"tags = {}\n"
]
<<~TEMPLATE
host "#{host_name}" {
#{option_contents.join(' ')}}
TEMPLATE
end
puts config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment