Class: Capistrano::SSH

### Home Classes Methods

### In Files

- lib/capistrano/ssh.rb

### Parent

Object

### Methods

- ::connect
- ::connection_strategy

### Files

- help.txt

### Class/Module Index

Quicksearch

- Capistrano
- Capistrano::CLI
- Capistrano::CLI::Execute
- Capistrano::CLI::Execute::ClassMethods
- Capistrano::CLI::Help
- Capistrano::CLI::Options
- Capistrano::CLI::Options::ClassMethods
- Capistrano::CLI::UI
- Capistrano::CLI::UI::ClassMethods
- Capistrano::Callback
- Capistrano::Command
- Capistrano::Command::Tree
- Capistrano::Command::Tree::Branch
- Capistrano::Command::Tree::ConditionBranch
- Capistrano::Command::Tree::ConditionBranch::Evaluator
- Capistrano::Command::Tree::ElseBranch
- Capistrano::Configuration
- Capistrano::Configuration::Actions
- Capistrano::Configuration::Actions::FileTransfer
- Capistrano::Configuration::Actions::Inspect
- Capistrano::Configuration::Actions::Invocation
- Capistrano::Configuration::Actions::Invocation::ClassMethods
- Capistrano::Configuration::AliasTask
- Capistrano::Configuration::Callbacks
- Capistrano::Configuration::Connections
- Capistrano::Configuration::Execution
- Capistrano::Configuration::Loading
- Capistrano::Configuration::Loading::ClassMethods
- Capistrano::Configuration::LogFormatters
- Capistrano::Configuration::Namespaces
- Capistrano::Configuration::Namespaces::Namespace
- Capistrano::Configuration::Roles
- Capistrano::Configuration::Servers
- Capistrano::Configuration::Variables
- Capistrano::Deploy
- Capistrano::Deploy::Dependencies
- Capistrano::Deploy::LocalDependency
- Capistrano::Deploy::RemoteDependency
- Capistrano::Deploy::SCM
- Capistrano::Deploy::SCM::Accurev
- Capistrano::Deploy::SCM::Accurev::InternalRevision
- Capistrano::Deploy::SCM::Base
- Capistrano::Deploy::SCM::Base::LocalProxy
- Capistrano::Deploy::SCM::Bzr
- Capistrano::Deploy::SCM::Cvs
- Capistrano::Deploy::SCM::Darcs
- Capistrano::Deploy::SCM::Git
- Capistrano::Deploy::SCM::Mercurial
- Capistrano::Deploy::SCM::None
- Capistrano::Deploy::SCM::Perforce
- Capistrano::Deploy::SCM::Subversion
- Capistrano::Deploy::Strategy
- Capistrano::Deploy::Strategy::Base
- Capistrano::Deploy::Strategy::Checkout
- Capistrano::Deploy::Strategy::Copy
- Capistrano::Deploy::Strategy::Export
- Capistrano::Deploy::Strategy::Remote
- Capistrano::Deploy::Strategy::RemoteCache
- Capistrano::Deploy::Strategy::UnsharedRemoteCache
- Capistrano::ProcCallback
- Capistrano::Processable
- Capistrano::Processable::SessionAssociation
- Capistrano::RemoteError
- Capistrano::Role
- Capistrano::Role::DynamicServerList
- Capistrano::SSH
- Capistrano::ServerDefinition
- Capistrano::Shell
- Capistrano::TaskCallback
- Capistrano::TaskDefinition
- Capistrano::Transfer
- Capistrano::Transfer::SFTPTransferWrapper
- Capistrano::Version
- Kernel
- Object
- String

No matching classes.

# Capistrano::SSH

A helper class for dealing with SSH connections.

### Public Class Methods

connect(server, options={}) click to toggle source

An abstraction to make it possible to connect to the server via public key without prompting for the password. If the public key authentication fails this will fall back to password authentication.

`server` must be an instance of ServerDefinition.

If a block is given, the new session is yielded to it, otherwise the new session is returned.

If an :ssh_options key exists in `options`, it is passed to the Net::SSH constructor. Values in `options` are then merged into it, and any connection information in `server` is added last, so that `server` info takes precedence over `options`, which takes precendence over ssh_options.

    # File lib/capistrano/ssh.rb, line 33
    def self.connect(server, options={})
      connection_strategy(server, options) do |host, user, connection_options|
        connection = Net::SSH.start(host, user, connection_options)
        Server.apply_to(connection, server)
      end
    end

connection_strategy(server, options={}, &block) click to toggle source

Abstracts the logic for establishing an SSH connection (which includes testing for connection failures and retrying with a password, and so forth, mostly made complicated because of the fact that some of these variables might be lazily evaluated and try to do something like prompt the user, which should only happen when absolutely necessary.

This will yield the hostname, username, and a hash of connection options to the given block, which should return a new connection.

    # File lib/capistrano/ssh.rb, line 48
    def self.connection_strategy(server, options={}, &block)
      methods = [ %(publickey hostbased), %(password keyboard-interactive) ]
      password_value = nil

      # construct the hash of ssh options that should be passed more-or-less
      # directly to Net::SSH. This will be the general ssh options, merged with
      # the server-specific ssh-options.
      ssh_options = (options[:ssh_options] || {}).merge(server.options[:ssh_options] || {})

      # load any SSH configuration files that were specified in the SSH options. This
      # will load from ~/.ssh/config and /etc/ssh_config by default (see Net::SSH
      # for details). Merge the explicitly given ssh_options over the top of the info
      # from the config file.
      ssh_options = Net::SSH.configuration_for(server.host, ssh_options.fetch(:config, true)).merge(ssh_options)

      # Once we've loaded the config, we don't need Net::SSH to do it again.
      ssh_options[:config] = false

      ssh_options[:verbose] = :debug if options[:verbose] && options[:verbose] > 0

      user = server.user || options[:user] || ssh_options[:username] ||
             ssh_options[:user] || ServerDefinition.default_user
      port = server.port || options[:port] || ssh_options[:port]

      # the .ssh/config file might have changed the host-name on us
      host = ssh_options.fetch(:host_name, server.host)

      ssh_options[:port] = port if port

      # delete these, since we've determined which username to use by this point
      ssh_options.delete(:username)
      ssh_options.delete(:user)

      begin
        connection_options = ssh_options.merge(
          :password => password_value,
          :auth_methods => ssh_options[:auth_methods] || methods.shift
        )

        yield host, user, connection_options
      rescue Net::SSH::AuthenticationFailed
        raise if methods.empty? || ssh_options[:auth_methods]
        password_value = options[:password]
        retry
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.