我正在尝试使用带有管理员凭据的Net::SSH
连接到服务器。登录后立即执行一个需要输入“是”或“否”的脚本,并根据输入执行下一个命令。
虽然我能够连接,但是在将"Yes\n"
发送到服务器后,循环不会结束。为什么呢?
require 'net/ssh'
Net::SSH.start(Host, 'root',:port => "26", :password => "pass") do |ssh|
ssh.open_channel do |channel|
channel.request_pty(:term => "xterm") do |ch, success|
if success
puts "pty successfully obtained"
ch.send_channel_request "shell" do |ch, success|
if success
ch.send_data("su admin\n")
ch.send_data("command\n")
ch.on_process do |data|
ch.send_data("yes\n")
ch.send_data("command\n")
end
else
puts "could not start user shell"
end
end
else
puts "could not obtain pty"
end
end
end
ssh.loop
end