Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'fileutils'
- username = ARGV[0].downcase
- password = ARGV[1]
- public_key = ARGV[2]
- # TODO: REMOVE THIS WHEN STABLE.
- puts "Calling deluser..."
- system("deluser", username)
- puts "Calling useradd..."
- system("useradd", username, "--create-home")
- puts "Setting up password..."
- # Ugly hackery...
- File.open("pass", "w+") do |f|
- f.write password
- end
- `usermod -p \`makepasswd --clearfrom=pass --crypt-md5 | awk '{print $2}'\` #{ARGV[0]}`
- FileUtils.rm("pass")
- # Glad that's over with!
- # Let the fun & games begin!
- puts "Setting up .ssh & public key"
- users_home = "/home/#{username}"
- authorized_keys = File.join(users_home, ".ssh", "authorized_keys")
- FileUtils.mkdir(File.join(users_home, ".ssh")) rescue nil
- FileUtils.chown(username, username, File.join(users_home, ".ssh"))
- FileUtils.chmod(0700, File.join(users_home, ".ssh"))
- File.open(File.join(authorized_keys), "w+") do |f|
- f.write public_key
- end
- FileUtils.chmod(0600, authorized_keys)
- FileUtils.chown(username, username, authorized_keys)
Add Comment
Please, Sign In to add comment