Created
April 27, 2015 09:21
-
-
Save toshihirock/42a674f5dff3b80c49b6 to your computer and use it in GitHub Desktop.
踏み台の先のサーバーで任意のコマンドを実行し、その終了ステータスを取得する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/ssh' | |
require 'fileutils' | |
PASSWORD="password" | |
CONFIG='./config_file' | |
def write_config_file | |
File.open(CONFIG, 'w') { |f| | |
f << "Host fumidai\n" | |
f << "HostName hoge\n" | |
f << "User fumidai-user\n" | |
f << "IdentityFile ~/Keys/id_rsa\n" | |
f << "\n" | |
f << "Host ikitai-server\n" | |
f << "HostName fuga\n" | |
f << "User ikitai-user\n" | |
f << "ProxyCommand ssh -W %h:%p fumidai" | |
} | |
end | |
write_config_file | |
result = "" | |
Net::SSH.start('ikitai-server', 'ikitai-user', :password=>PASSWORD, :config => CONFIG){|ssh| | |
ssh.open_channel do |channel| | |
channel.exec('nc -w 1 localhost 22') | |
channel.on_request 'exit-status' do |ch, data| | |
result = data.read_long | |
end | |
end | |
} | |
p result | |
FileUtils.rm(CONFIG, {:force => true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment