如何在同一个ssh会话中执行2个或更多命令?

kam*_*mal 5 ruby

我有以下脚本:

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

Net::SSH.start('host1', 'root', :password => "mypassword1") do |ssh|
    stdout = ""

    ssh.exec("cd /var/example/engines/")
    ssh.exec!( "pwd" ) do |channel, stream, data|
        stdout << data if stream == :stdout
    end
    puts stdout

    ssh.loop
end
Run Code Online (Sandbox Code Playgroud)

我得到了/root,而不是 /var/example/engines/

nru*_*uth 1

看看是否有类似于 file(utils?) cd 块语法的内容,否则只需在同一个子 shell 中运行命令,例如 ssh.exec "cd /var/example/engines/; pwd" ?