如何通过 Net::SSH (ruby) 连接到 xen 控制台
我正在尝试编写一些可以连接到 XenServer VM 控制台(通过 /usr/lib/xen/bin/xenconsole)的 ruby 代码,首先通过 SSH 连接到主机服务器,然后从命令行。
为此,我使用 ruby 库 Net::SSH
。我可以通过 SSH 登录主机服务器并运行命令来获取虚拟机的 DOM id。当我运行 xenconsole 命令时出现问题。您必须在命令后按“Enter”键才能转储到控制台,然后必须按 CTRL + ]
退出虚拟机的控制台并返回主机的命令行。
我正在使用下面的代码,但它挂在“按 Enter”点,并且不会从 SSH 通道提供任何反馈(作为 STDOUT 或 STDERR)。如何访问虚拟机控制台并在虚拟机上执行命令?那么如何发送 CTRL + ]
字符呢?
def execute_remote_console(hostname, port, username, password, uuid)
begin
Net::SSH.start( hostname, username, :password => password, :port => port ) do |session|
dom_list_line = session.exec! "list_domains | grep #{uuid}"
if dom_list_line.match(/(\d+)/)
dom_id = $1
puts "found #{uuid} on DOM #{dom_id}"
else
raise "couldn't find DOM id"
end
console_command = "/usr/lib/xen/bin/xenconsole #{dom_id}"
puts "connecting to console: #{console_command}"
session.exec!( console_command ) do |ch,stream,data|
puts "pressing (enter)"
ch.send_data "\n"
case stream
when :stderr
puts "E-> #{data}"
ch.exec "cat /etc/hostname" do |chan, success|
raise "could not execute command" unless success
# "on_data" is called when the process writes something to stdout
chan.on_data do |c, data|
$STDOUT.print data
end
# "on_extended_data" is called when the process writes something to stderr
chan.on_extended_data do |c, type, data|
$STDERR.print data
end
chan.on_close { puts "done!" }
end
when :stdout
puts "O-> #{data}"
else
puts" other: #{data}"
end
end #end session.exec
end #end SSH.start
rescue
puts "\t\t\tok (#{$!.message})"
end
end #end function
I'm trying to write some ruby code that could connect to a XenServer VMs console (via /usr/lib/xen/bin/xenconsole
) by first SSHing into the Host server then accessing the VMs console from the command line.
To do this I'm using the ruby library Net::SSH
. I'm doing ok logging into the Host server via SSH and running the commands to get the DOM id of the VM. The problem comes when I'm running the xenconsole command. You have to press "enter" after the command to get dumped into the console, then you have to press CTRL + ]
to exit the VM's console and get back to the Host's command line.
I'm using the code below, but it hangs at the "press enter" point and doesn't give any feedback from the SSH channel either as STDOUT or STDERR. What can I do to get to the VM's console to execute commands on the VM? Then how do I send the CTRL + ]
characters?
def execute_remote_console(hostname, port, username, password, uuid)
begin
Net::SSH.start( hostname, username, :password => password, :port => port ) do |session|
dom_list_line = session.exec! "list_domains | grep #{uuid}"
if dom_list_line.match(/(\d+)/)
dom_id = $1
puts "found #{uuid} on DOM #{dom_id}"
else
raise "couldn't find DOM id"
end
console_command = "/usr/lib/xen/bin/xenconsole #{dom_id}"
puts "connecting to console: #{console_command}"
session.exec!( console_command ) do |ch,stream,data|
puts "pressing (enter)"
ch.send_data "\n"
case stream
when :stderr
puts "E-> #{data}"
ch.exec "cat /etc/hostname" do |chan, success|
raise "could not execute command" unless success
# "on_data" is called when the process writes something to stdout
chan.on_data do |c, data|
$STDOUT.print data
end
# "on_extended_data" is called when the process writes something to stderr
chan.on_extended_data do |c, type, data|
$STDERR.print data
end
chan.on_close { puts "done!" }
end
when :stdout
puts "O-> #{data}"
else
puts" other: #{data}"
end
end #end session.exec
end #end SSH.start
rescue
puts "\t\t\tok (#{$!.message})"
end
end #end function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论