class PDO::SSHCmd
Public Class Methods
new(local, sshopts)
click to toggle source
# File lib/pdo/task.rb, line 9 def initialize(local, sshopts) @defaults = { :user => 'root', :port => '22', :ssh => '/usr/bin/ssh', :sshopts => { :ConnectTimeout => '60', :StrictHostKeyChecking => 'no', }, } @local = local @sshopts = @defaults[:sshopts] @sshopts = @defaults[:sshopts].update sshopts if sshopts logger.debug { @sshopts.inspect } end
Public Instance Methods
form(host, cmd)
click to toggle source
# File lib/pdo/task.rb, line 26 def form(host, cmd) if host.match(/^(?:(\w+)@)?(\w+(?:\.\w+)*)(?::(\d+))?$/) then user, host, port = $1, $2, $3 end cmd = cmd.dup # without dup, all tasks refer to the same cmd.strip! cmd.gsub! '_USER_', user ? user : @defaults[:user] cmd.gsub! '_HOST_', host cmd.gsub! '_PORT_', port ? port : @defaults[:port] # when opts.cmd == '-', the command is read from stdin. # in this case multiple lines can be entered. here I'm doing some # simple substitution for "\n". cmd.gsub! "\n", '; ' # escape "`$ characters in cmd, unless they're already escaped. if RUBY_VERSION.to_f < 1.9 then # v1.8 dose not support negative look-behind assertion, thus # doing it in 2 steps. if cmd.match /["`$]/ then if not cmd.match /['\\]["`$]/ then cmd.gsub! /(["`$])/, '\\\\\1' end end else cmd.gsub! /(?<!['\\])(["`$])/, '\\\\\1' end if @local then return cmd else ssh = @defaults[:ssh] ssh = [ ssh, '-l', "#{user}"].join ' ' if user ssh = [ ssh, '-p', "#{port}"].join ' ' if port @sshopts.each do |k, v| ssh = [ssh, '-o', "#{k}=#{v}"].join ' ' end ssh = [ ssh, "#{host}", "\"#{cmd}\""].join ' ' return ssh end end