Module: RunSSHLib::SshBackend

Defined in:
lib/runsshlib/ssh_backend.rb

Overview

A collection of ssh procedures.

Defined Under Namespace

Classes: KnownHostsUtils

Class Method Summary collapse

Class Method Details

.copy_id(options) ⇒ Object

Copy ssh identity file to remote host according to options

options

A hash containing host definition (:host_name, etc) and optionally identity_file path.



84
85
86
87
88
89
90
# File 'lib/runsshlib/ssh_backend.rb', line 84

def copy_id(options)
  command = "ssh-copy-id "
  command << "-i #{options[:identity_file]} " if options[:identity_file]
  command << "#{options[:login]}@" if options[:login]
  command << "#{options[:host_name]}"
  exec command
end

.normalize_scp_targets(targets, host_name) ⇒ Object

Prepend the remote target with supplied host_name.

Raises ParametersError if no target is indicated as remote (with ‘:’) or if the number of targets is not 2.

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/runsshlib/ssh_backend.rb', line 105

def normalize_scp_targets(targets, host_name)
  raise ParametersError, "Invalid targets: #{targets.join(' ')}" unless
        targets.length == 2
  raise "no hostname" unless host_name # should never happen

  if targets[0].start_with?(':')
    targets[0] = host_name + targets[0]
  elsif targets[1].start_with?(':')
    targets[1] = host_name + targets[1]
  else
    raise ParametersError, "The remote path should be prefixed with ':'!"
  end
  targets
end

.normalize_tunnel_definition(tunnel_definition) ⇒ Object

Accepts abbriviated or full definition of ssh tunnel definition and converts it to full tunnel definition. If only port is supplied (abbriviated form) it uses localhost as the hostname and the same port on both end of the tunnel definition.



96
97
98
99
# File 'lib/runsshlib/ssh_backend.rb', line 96

def normalize_tunnel_definition(tunnel_definition)
  tunnel_definition =~ /(^\d+$)/ ? "#{$1}:localhost:#{$1}" :
                       tunnel_definition
end

.scp(definition) ⇒ Object

Copy files to/from remote host using scp. definition: A hash containig a merge between host definition and

user input.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/runsshlib/ssh_backend.rb', line 65

def scp(definition)
  raise "no hostname" unless definition[:host_name] # should never happen
  command = "scp"
  arguments = []
  arguments << '-r' if definition[:recurssive_given]
  arguments += ['-l', definition[:limit]] if definition[:limit]
  definition[:option].each do |option|
    arguments += ['-o', option]
  end if definition[:option]
  host_name = definition[:login] ?
              "#{definition[:login]}@#{definition[:host_name]}" :
              definition[:host_name]
  arguments += normalize_scp_targets(definition[:targets], host_name)
  exec command, *arguments
end

.shell(definition) ⇒ Object

run shell on remote host.

definition

A Hash containing required data for making shell connection (e.g., :host_name, :login).

For running remote commands add to the definition hash the entire remote command as a string with a :remote_cmd key.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/runsshlib/ssh_backend.rb', line 46

def shell(definition)
  raise "no hostname" unless definition[:host_name] # should never happen
  rmtcmd_flag = (definition[:remote_cmd] && (!definition[:remote_cmd].empty?))
  command = "ssh "
  command << "-t " if (rmtcmd_flag && (!definition[:no_pseudo_terminal]))
  command << "-l #{definition[:login]} " if definition[:login]
  command << "#{definition[:host_name]}"
  command << " -L #{normalize_tunnel_definition definition[:local_tunnel]}" if
             definition[:local_tunnel]
  definition[:option].each do |option|
    command << " -o #{option}"
  end if definition[:option]
  command << %( -- "#{definition[:remote_cmd]}") if rmtcmd_flag
  exec command
end