sshkit なるもの
今日はサーバにsshで入ってごにょごにょする処理をsshkitを使ってスクリプト化しているものに出会ったので、それをまとめておく。
sshkitというもので、capistranoが内部で使っているDSLとのこと。
https://github.com/capistrano/sshkit
https://github.com/capistrano/sshkit/blob/master/EXAMPLES.md
複数のサーバにsshで入って、並列でgrepを実行して結果をローカルに保存するrubyスクリプトが下記のようになる。
#!/usr/bin/env ruby # encoding: UTF-8 require 'sshkit/dsl' servers = ['kidaaam@server-host01', 'kidaaam@server-host02'] foo = ARGV.shift on servers, in: :parallel do within '/tmp/log/' do output = capture('grep', '-h', '-E', ".*:[^\\d\\w\\s]#{foo}[^\\d\\w\\s]", 'hoge.log', '|', 'cat') File.open("#{host}.log", 'w') do |f| f.write(output) end end end