Guest User

Untitled

a guest
Jun 3rd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. require 'net/ssh'
  2.  
  3. host = "the.host"
  4. user = "joe"
  5. su_user = "bob"
  6. password = "password"
  7.  
  8. commands = ["cd /", "pwd", "ls -l", "exit"]
  9. finished = ("%08x" * 8) % Array.new(8) { rand(0xFFFFFFFF) }
  10.  
  11. Net::SSH.start(host, user) do |ssh|
  12. ssh.open_channel do |channel|
  13. channel.request_pty(:modes => { Net::SSH::Connection::Term::ECHO => 0 }) do |c, success|
  14. raise "could not request pty" unless success
  15.  
  16. channel.on_data do |c_, data|
  17. if data =~ /^Password:/
  18. channel.send_data(password + "\n")
  19. channel.send_data(commands.shift + "; echo #{finished}\n")
  20. elsif data.include?(finished)
  21. channel.send_data(commands.shift + "; echo #{finished}\n")
  22. else
  23. puts data
  24. end
  25. end
  26.  
  27. channel.exec "su #{su_user}"
  28. end
  29. end
  30.  
  31. ssh.loop
  32. end
Add Comment
Please, Sign In to add comment