Guest User

Untitled

a guest
Mar 3rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. require 'fileutils'
  2.  
  3. username = ARGV[0].downcase
  4. password = ARGV[1]
  5. public_key = ARGV[2]
  6.  
  7. # TODO: REMOVE THIS WHEN STABLE.
  8. puts "Calling deluser..."
  9. system("deluser", username)
  10.  
  11. puts "Calling useradd..."
  12. system("useradd", username, "--create-home")
  13.  
  14. puts "Setting up password..."
  15. # Ugly hackery...
  16. File.open("pass", "w+") do |f|
  17. f.write password
  18. end
  19. `usermod -p \`makepasswd --clearfrom=pass --crypt-md5 | awk '{print $2}'\` #{ARGV[0]}`
  20. FileUtils.rm("pass")
  21. # Glad that's over with!
  22.  
  23. # Let the fun & games begin!
  24. puts "Setting up .ssh & public key"
  25. users_home = "/home/#{username}"
  26. authorized_keys = File.join(users_home, ".ssh", "authorized_keys")
  27. FileUtils.mkdir(File.join(users_home, ".ssh")) rescue nil
  28. FileUtils.chown(username, username, File.join(users_home, ".ssh"))
  29. FileUtils.chmod(0700, File.join(users_home, ".ssh"))
  30. File.open(File.join(authorized_keys), "w+") do |f|
  31. f.write public_key
  32. end
  33. FileUtils.chmod(0600, authorized_keys)
  34. FileUtils.chown(username, username, authorized_keys)
Add Comment
Please, Sign In to add comment