-
-
Save windwiny/04bf3f591b9044cd92c7 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" | |
su_password = "password" | |
commands = ['export PS1="\n[$?] [\t] $PS1----\n"', "cd /", "pwd", "ls -l", "exit"] | |
finished = ("%08x" * 8) % Array.new(8) { rand(0xFFFFFFFF) } | |
Net::SSH.start(host, user, password: password) 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(su_password + "\n") | |
channel.send_data(commands.shift + "; echo #{finished}\n") | |
elsif data.include?(finished) | |
channel.send_data(commands.shift + "; echo #{finished}\n") | |
else | |
print data | |
end | |
end | |
channel.exec "su - #{su_user}" | |
end | |
end | |
ssh.loop | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment