3

I'm writing a ruby on rails service that will conect to various servers via SSH.

I want to generate the public/rivate key and store them in the database. Then the user will be able to view the public key and add it to there key authentication for SSH on their server.

The my service will contact the servers via Net::SSH and present the corresponding keys.

My questions is what API calls do I need to achive this. Most of the documentation assomes you'll be creating keys in the .ssh directory.

Ian Purton
  • 15,331
  • 2
  • 27
  • 26

3 Answers3

3

This works for generating private and public keys in Ruby

require 'openssl'


key = OpenSSL::PKey::RSA.generate(2048)
key.public_key

puts key.public_key
puts key

So I think that the answer that is required is 'openssl'

Adjam
  • 598
  • 1
  • 8
  • 22
  • 1
    `OpenSSL` and `OpenSSH` rsa pair keys have a different format... see [this question](https://stackoverflow.com/q/5270386/4352306) – rellampec Aug 02 '18 at 10:26
2

I found out how to directly pass the keys to Net::SSH

Basically you can pass a PEM format private key using the :key_data option. Net::SSH can then generate the public key from that as it needs both.

Ian Purton
  • 15,331
  • 2
  • 27
  • 26
-1

Can't you use ssh-keygen command ? I mean:

get passphrase and other parameters from web interface

validate, check etc. and then:

#!/usr/bin/env ruby
$VERBOSE=true

`ssh-keygen -f ~/sshkeyfile -t rsa -C "something"  -P "something else"`

Of course changing path to something more appropriate, and using options that you want.

Then read sshkeyfile and sshkeyfile.pub from your choosen location
store it in db etc.

Casual Coder
  • 1,492
  • 2
  • 14
  • 15
  • I suppose that is one way to do it. I was hoping to stay away from the filesystem. i.e. Create the keys into a string and then store. Then when conenctiong with Net::SSH present the keys as a string. – Ian Purton Sep 23 '10 at 13:02
  • Look at http://stackoverflow.com/questions/235759/opening-an-rsa-private-key-from-ruby – Casual Coder Sep 23 '10 at 13:14