Created
January 2, 2014 07:13
-
-
Save jjam3774/8215924 to your computer and use it in GitHub Desktop.
A more complete version of the previous script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby2.0 -v | |
require 'rubygems' | |
require 'pty' | |
require 'expect' | |
require 'highline/import' | |
$expect_verbose = true #To see the output of the session | |
#VERBOSE=nil | |
def 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("") | |
#rscreen.read | |
} | |
end | |
def copy_key | |
puts "Userid: " | |
userid = gets | |
pass = ask("Enter your password: ") { |q| q.echo = "*" } | |
begin | |
File.open('hostfile').each { |i| | |
puts "Copying key to #{i}" | |
PTY.spawn("ssh-copy-id -i #{userid.chomp}@#{i.chomp}"){ |rscreen, wscreen, pid| | |
wscreen.sync = true | |
rscreen.sync = true | |
if rscreen.expect(/Are/, 1) | |
wscreen.puts('yes') | |
rscreen.expect(/[Pp]assword/) | |
wscreen.puts(pass.chomp) | |
rscreen.expect(/[#$%]/,1) | |
else | |
rscreen.expect(/[Pp]assword:/) | |
wscreen.puts(pass.chomp) | |
if rscreen.expect(/[#$%]/,1) | |
puts "Complete...." | |
end | |
end | |
} | |
} | |
rescue | |
puts File.exists?('hostfile') ? "hostfile exists." : "the hostfile does not exist" | |
end | |
end | |
puts "Create a new key? " | |
ans = gets() | |
if ans.chomp == "y" | |
puts File.exists?("/root/.ssh/id_rsa.pub") ? "Key already exists..." : create_key | |
copy_key | |
else | |
copy_key | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment