09 - controlling a process on another machine.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 27 行

RB
27
字号
require 'rubygems'require 'net/ssh'def run_remotely(command, host, args)  Net::SSH.start(host, args) do |session|        session.process.popen3(command) do |stdin, stdout, stderr|      yield stdin, stdout, stderr    end  endend#---run_remotely('ls -l /home/leonardr/dir', 'example.com', :username=>'leonardr',             :password => 'mypass') { |i, o, e| puts o.read }# -rw-rw-r--    1 leonardr leonardr       33 Dec 29 20:40 file1# -rw-rw-r--    1 leonardr leonardr      102 Dec 29 20:40 file2#---run_remotely('cat', 'example.com', :username=>'leonardr',             :password => 'mypass') do |stdin, stdout, stderr|  stdin.puts 'Line one.'  puts stdout.read  stdin.puts 'Line two.'  puts stdout.readend# "Line one."# "Line two."#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?