Advertisement
Guest User

Untitled

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