Guest User

Untitled

a guest
Apr 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3. require 'rubygems'
  4. require 'net/ssh'
  5.  
  6. print "User: "
  7. user = gets.chomp!
  8.  
  9. print "Password: "
  10. password = gets.chomp!
  11.  
  12. print "Host: "
  13. host = gets.chomp!
  14.  
  15. print "Port: "
  16. port = gets.chomp!
  17.  
  18. puts "Checking host..."
  19.  
  20. opts = { :password => password }
  21. if port.to_i > 0
  22. opts[:port] = port
  23. end
  24.  
  25. Net::SSH.start(host, user, opts) do |ssh|
  26. result = ssh.exec!('ls')
  27. puts result
  28.  
  29. # polaczony, tworzymy klucze
  30. puts "[+] Generating key for #{user}@#{host}"
  31. %x(ssh-keygen -t dsa -f #{host})
  32.  
  33. pub_key = "#{host}.pub"
  34. priv_key = host
  35.  
  36. if !File.exists?(priv_key)
  37. raise "Nie utworzono klucza prywatnego"
  38. end
  39. if !File.exists?(pub_key)
  40. raise "Nie utworzono klucza publicznego"
  41. end
  42.  
  43. pub_key_data = IO.read(pub_key)
  44.  
  45. %x(mv #{priv_key} $HOME/.ssh/)
  46. %x(mv #{pub_key} $HOME/.ssh/)
  47.  
  48. puts pub_key_data.class
  49.  
  50. ssh.exec!('[ -d "$HOME/.ssh" ] || (echo "Nie ma katalogu ~/.ssh - tworze"; mkdir $HOME/.ssh; chmod 700 $HOME/.ssh)')
  51. ssh.exec!("echo -n \"#{pub_key_data}\" >> $HOME/.ssh/authorized_keys && chmod 600 $HOME/.ssh/authorized_keys")
  52.  
  53. ssh_config = "#{ENV['HOME']}/.ssh/config"
  54.  
  55. puts "[+] Dopisuje do konfiga"
  56. File.open(ssh_config, 'a') do |file|
  57. file.puts
  58. file.puts "# Automatic added at #{Time.now}"
  59. file.puts "Host #{host}"
  60. file.puts "User #{user}"
  61. if port.to_i > 0
  62. file.puts "Port #{port}"
  63. end
  64. file.puts "IdentityFile ~/.ssh/#{host}"
  65. end
  66.  
  67. puts "[+] Done"
  68. puts "[+] ssh #{user}@#{host}"
  69. end
Add Comment
Please, Sign In to add comment