LoginSignup
25
24

More than 5 years have passed since last update.

sshkitを単体で使ってみる

Last updated at Posted at 2014-01-14

capistrano3でよく使うrun_locallyやon hostsなどのDSLは、capistranoが内部で使っているsshkitが提供しています。

capistranoのようにデプロイに特化しているわけでもないけど、localとremoteを簡単に区別し、さらに複数台相手に同じ処理をさせたい場合などに、sshkitは使えると思います。。

githubにgetting started的なものがないので、コードを見ながらサンプルを書いてみました。

Gemfile
source 'https://rubygems.org'
gem 'sshkit'
sshkit_sample.rb
require 'sshkit'
require 'sshkit/dsl'

SSHKit.config.output_verbosity = Logger::DEBUG

remote_host = SSHKit::Host.new('192.168.20.10')
remote_host.user = "vagrant"
remote_host.ssh_options = {
  keys: %w(./id_rsa),
  auth_methods: %w(publickey)
}

on remote_host do
  execute :ls
end

run_locally do
  execute :whoami
end

実行結果

$ ruby sshkit_sample.rb
INFO [21c7bce1] Running /usr/bin/env ls on 192.168.20.10
DEBUG [21c7bce1] Command: /usr/bin/env ls
INFO [21c7bce1] Finished in 0.182 seconds with exit status 0 (successful).
DEBUG [21c7bce1] apt.sh
DEBUG [21c7bce1] build_time.sh
DEBUG [21c7bce1] cleanup.sh
DEBUG [21c7bce1] sudo.sh
DEBUG [21c7bce1] vagrant.sh
DEBUG [21c7bce1] vbox.sh
INFO [21c7bce1] Finished in 0.182 seconds with exit status 0 (successful).
INFO [69ec5244] Running /usr/bin/env whoami on
DEBUG [69ec5244] Command: /usr/bin/env whoami
DEBUG [69ec5244] cazador
INFO [69ec5244] Finished in 0.006 seconds with exit status 0 (successful).

ちなみにon hogehogeやrun_locallyの実装はsshkit/dsl.rbにあります。

公式にサンプル があるので、参考になると思います。

25
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24