|
|||||
|
対象: net-sshでリモートプロセスの終了ステータスを取得net-sshでリモートのシェルスクリプトやコマンドの終了ステータスを取得するには、open_channelメソッドでオープンしたチャネルにon_requestメソッドでブロックを登録する。 on_requestメソッドの引数には"exit-status"を指定し、ブロックに渡されたdataに対してread_longメソッドを使えばリモートプロセスの終了ステータスを取得できる。
require "rubygems"
require "net/ssh"
Net::SSH.start("localhost" , "mobile", :password => "mobile") do |ssh|
wch = ssh.open_channel do |channel|
channel.exec("./exit.sh") do |ch, success|
channel.on_data {|ch, data| print data}
channel.on_extended_data {|ch, type, data| print data}
# data.read_longでリモートプロセスの終了ステータスが取得できる
channel.on_request("exit-status") {|ch, data| print "exit-status is #{data.read_long}\n"}
end
end
wch.wait # リモートプロセスの終了まで待機する
end
上記スクリプトssh_exit_status.rbで呼び出しているシェルスクリプトexit.shは以下のようなものである。 #!/bin/sh echo "exit status test" exit 99 これを実行すると以下のようにsshしてexecしたシェルスクリプトの終了ステータスが得られる。 [mobile@localhost ruby]$ jruby ssh_exit_status.rb exit status test exit-status is 99Net::SSH::Connection::Channel.on_request (2010/03/29)
Copyright(C) 2004-2014 モバイル開発系(K) All rights reserved.
[Home]
|
|||||