Class: SftpWrapper::OpenSSH

Inherits:
Object
  • Object
show all
Defined in:
lib/sftp_wrapper/open_ssh.rb

Overview

The wrapper for OpenSSH’s sftp command.

Constant Summary collapse

DEFAULT_OPEN_TIMEOUT =

Default value of open timeout.

10
DEFAULT_READ_TIMEOUT =

Default value of read timeout.

10
DEFAULT_COMMAND_TIMEOUT =

Default value of command timeout.

9_999_999
DEFAULT_SSH_OPTIONS =

Default value of ssh options

{
  PasswordAuthentication: 'yes',
  PreferredAuthentications: 'password',
  PubkeyAuthentication: 'no',
  NumberOfPasswordPrompts: 1,
  StrictHostKeyChecking: 'no',
  UserKnownHostsFile: '/dev/null',
  TCPKeepAlive: 'yes',
  ServerAliveInterval: 60,
  ServerAliveCountMax: 3,
}.freeze
DEFAULT_SSH_CONFIG =

Default value of ssh config file path.

'/dev/null'
PASSWORD_PROMPT_RE =

pattern of password prompt.

/ password: /.freeze
SFTP_PROMPT =

sftp prompt.

'sftp> '
SFTP_PROMPT_RE =

pattern of sftp prompt.

/^#{SFTP_PROMPT}/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, username, password, ssh_options: {}, ssh_config: DEFAULT_SSH_CONFIG, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, command_timeout: DEFAULT_COMMAND_TIMEOUT, debug: false) ⇒ OpenSSH

Initialize SFTP wrapper.

rubocop:disable Metrics/ParameterLists

Parameters:

  • host (String)

    host address of SFTP server

  • port (Integer)

    port number of SFTP server

  • username (String)

    user name of SFTP server

  • password (String)

    password of SFTP server

  • ssh_options (Hash) (defaults to: {})

    SSH options (set as -o options)

  • ssh_config (String) (defaults to: DEFAULT_SSH_CONFIG)

    path of SSH config file (set as -F option unless nil)

  • open_timeout (Integer, Float) (defaults to: DEFAULT_OPEN_TIMEOUT)
  • read_timeout (Integer, Float) (defaults to: DEFAULT_READ_TIMEOUT)
  • command_timeout (Integer, Float) (defaults to: DEFAULT_COMMAND_TIMEOUT)
  • debug (Boolean) (defaults to: false)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sftp_wrapper/open_ssh.rb', line 62

def initialize(host, port, username, password,
               ssh_options: {},
               ssh_config: DEFAULT_SSH_CONFIG,
               open_timeout: DEFAULT_OPEN_TIMEOUT,
               read_timeout: DEFAULT_READ_TIMEOUT,
               command_timeout: DEFAULT_COMMAND_TIMEOUT,
               debug: false)
  @host = host
  @port = port
  @username = username
  @password = password
  @ssh_options = DEFAULT_SSH_OPTIONS.merge(ssh_options)
  @ssh_config = ssh_config
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @command_timeout = command_timeout
  @debug = debug
end

Instance Attribute Details

#command_timeoutObject (readonly)

Returns the value of attribute command_timeout.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def command_timeout
  @command_timeout
end

#debugObject (readonly)

Returns the value of attribute debug.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def debug
  @debug
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def host
  @host
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def open_timeout
  @open_timeout
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def port
  @port
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def read_timeout
  @read_timeout
end

#ssh_configObject (readonly)

Returns the value of attribute ssh_config.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def ssh_config
  @ssh_config
end

#ssh_optionsObject (readonly)

Returns the value of attribute ssh_options.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def ssh_options
  @ssh_options
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/sftp_wrapper/open_ssh.rb', line 10

def username
  @username
end

Instance Method Details

#download(source, destination) ⇒ Object

Get remote file.

Parameters:

  • source (String)

    source file path

  • destination (String)

    destination path

Raises:



91
92
93
# File 'lib/sftp_wrapper/open_ssh.rb', line 91

def download(source, destination)
  execute(%W[get #{source} #{destination}].shelljoin)
end

#upload(source, destination) ⇒ Object

Put local file.

Parameters:

  • source (String)

    source file path

  • destination (String)

    destination path

Raises:



104
105
106
# File 'lib/sftp_wrapper/open_ssh.rb', line 104

def upload(source, destination)
  execute(%W[put #{source} #{destination}].shelljoin)
end