Skip to content

Instantly share code, notes, and snippets.

@jjam3774
Last active July 29, 2020 23:13
Show Gist options
  • Save jjam3774/8215683 to your computer and use it in GitHub Desktop.
Save jjam3774/8215683 to your computer and use it in GitHub Desktop.
A Utility that will allow you to create and copy authentication keys to your remote servers.
#!/usr/bin/ruby
require 'rubygems'
require 'pty'
require 'expect'
require 'highline/import'
module SSHKeyUtil
$expect_verbose = true
$VERBOSE=true
def self.create_key
puts "Preparing to create key to transfer to remote systems..."
PTY.spawn('ssh-keygen -t rsa') { |rscreen, wscreen, pid|
rscreen.expect(/Enter/)
wscreen.puts("")
rscreen.expect(/Enter/)
wscreen.puts("")
rscreen.expect(/Enter/)
wscreen.puts("")
}
end
def self.copy_key
puts "LDAP Userid: "
user = gets
pass = ask("Enter your password: ") { |q| q.echo = "*" }
File.open('hostfile').each { |i|
puts "Copying key to #{i}"
PTY.spawn("ssh-copy-id -i #{user.chomp}@#{i.chomp}"){ |rscreen, wscreen, pid|
rscreen.expect(/The/)
wscreen.puts('yes')
rscreen.expect(/password/)
wscreen.puts(pass)
rscreen.expect(/[#$%]/)
wscreen.puts
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment