-
-
Save prashanth-sams/e7d90e1fc8e810eb270a9273238840c9 to your computer and use it in GitHub Desktop.
ex. SSH with real PTY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/ssh' | |
host = "the.host" | |
user = "joe" | |
su_user = "bob" | |
password = "password" | |
commands = ["cd /", "pwd", "ls -l", "exit"] | |
finished = ("%08x" * 8) % Array.new(8) { rand(0xFFFFFFFF) } | |
Net::SSH.start(host, user) do |ssh| | |
ssh.open_channel do |channel| | |
channel.request_pty(:modes => { Net::SSH::Connection::Term::ECHO => 0 }) do |c, success| | |
raise "could not request pty" unless success | |
channel.on_data do |c_, data| | |
if data =~ /^Password:/ | |
channel.send_data(password + "\n") | |
channel.send_data(commands.shift + "; echo #{finished}\n") | |
elsif data.include?(finished) | |
channel.send_data(commands.shift + "; echo #{finished}\n") | |
else | |
puts data | |
end | |
end | |
channel.exec "su #{su_user}" | |
end | |
end | |
ssh.loop | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment