Ian Purton
Ian Purton

Reputation: 15891

How can I create SSH public/private key pairs in ruby and store them in the Db

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.

Upvotes: 5

Views: 5333

Answers (3)

Adjam
Adjam

Reputation: 640

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'

Upvotes: 5

Ian Purton
Ian Purton

Reputation: 15891

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.

Upvotes: 3

Casual Coder
Casual Coder

Reputation: 1492

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.

Upvotes: -1

Related Questions