ruby - 如何在同一个 ssh session 中执行 2 个或更多命令?

标签 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

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

最佳答案

ssh.exec("cd /var/example/engines/; pwd")

这将执行 cd 命令,然后在新目录中执行 pwd 命令。

我不是 ruby 专家,但我猜可能有更优雅的解决方案。

关于ruby - 如何在同一个 ssh session 中执行 2 个或更多命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3671392/

相关文章:

ruby - 在 RSpec 中检查对测试替身的方法调用的参数

Ruby && 运算符结果没有意义

python - 由于填充检查失败,Ruby OpenSSL RSA 解密失败

mysql - rails 4 : Using ActiveRecord object from one thread in another

ruby - 如何在 cucumber 步骤中用表格写表格

php - 你能在 PHP 中使用全局方法吗?

ruby-on-rails - Kaminari 和额外参数

java - 使用 Ruby 将文件复制到 jar 文件中

ruby - 在 irb 中抑制 Enumerable::each 的输出

ruby - 如何用Puppet和ruby打印所有linux用户?