Class: Ghaki::NetSSH::Shell

Inherits:
Net::SSH
  • Object
show all
Includes:
Logger
Defined in:
lib/ghaki/net_ssh/shell.rb

Constant Summary collapse

DEF_TIMEOUT =
30
DEF_AUTH_METHODS =
%w{
  password
  keyboard-interactive
  publickey
  hostbased
}

Constants included from Logger

Logger::NO_OUTPUT

Instance Attribute Summary collapse

Attributes included from Logger

#should_log_command, #should_log_output

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log_all_off, #log_all_on, #log_command!, #log_command_off, #log_command_on, #log_exec!, #log_output_off, #log_output_on, #setup_logger

Constructor Details

#initialize(obj, opts = {}) ⇒ Shell

Returns a new instance of Shell.



118
119
120
121
122
123
# File 'lib/ghaki/net_ssh/shell.rb', line 118

def initialize obj, opts={}
  setup_logger opts
  @account = opts[:account]
  @raw_ssh = obj
  super obj
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



116
117
118
# File 'lib/ghaki/net_ssh/shell.rb', line 116

def 
  @account
end

#raw_sshObject

Returns the value of attribute raw_ssh.



116
117
118
# File 'lib/ghaki/net_ssh/shell.rb', line 116

def raw_ssh
  @raw_ssh
end

Class Method Details

.start(*args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ghaki/net_ssh/shell.rb', line 88

def self.start *args
  cur_opts = args_to_opts( args )
  acc = ( args, cur_opts )
  raw_opts = raw_opts_setup( cur_opts )
  begin
    raw_ssh = ::Net::SSH.start( acc.hostname, acc.username, raw_opts )
    cur_ssh = Ghaki::NetSSH::Shell.new( raw_ssh, cur_opts )
    if block_given?
      begin yield cur_ssh ensure raw_ssh.close end
    else
      return cur_ssh
    end
  rescue ::Net::SSH::HostKeyMismatch
    $!.remember_host!
    retry
  rescue Net::SSH::AuthenticationFailed
    if acc.retry_password?
      acc.fail_password
      unless cur_opts[:logger].nil?
        cur_opts[:logger].warn "failed password attempt for: #{acc}"
      end
      retry
    else
      raise
    end
  end
end

Instance Method Details

#discover(cmd, lookup) ⇒ Object



165
166
167
168
169
# File 'lib/ghaki/net_ssh/shell.rb', line 165

def discover cmd, lookup
  return lookup.match_lines( self.exec!(cmd).split("\n") ) do
    raise RemoteCommandError, 'SSH Discovery Output Not Matched'
  end
end

#download!(rem_file, loc_file) ⇒ Object



161
162
163
# File 'lib/ghaki/net_ssh/shell.rb', line 161

def download! rem_file, loc_file
  sftp.download! rem_file, loc_file
end

#exec!(cmd) ⇒ Object



147
148
149
150
151
# File 'lib/ghaki/net_ssh/shell.rb', line 147

def exec! cmd
  self.log_exec! 'SSH', cmd do
    @raw_ssh.exec!( cmd )
  end
end

#redirect(rem_file, loc_file, &block) ⇒ Object



171
172
173
174
175
176
# File 'lib/ghaki/net_ssh/shell.rb', line 171

def redirect rem_file, loc_file, &block
  sftp.remove! rem_file
  out = block.call
  sftp.download! rem_file, loc_file
  out
end

#remove!(rem_file) ⇒ Object



153
154
155
# File 'lib/ghaki/net_ssh/shell.rb', line 153

def remove! rem_file
  sftp.remove! rem_file
end

#sftpObject



125
126
127
128
129
130
131
132
# File 'lib/ghaki/net_ssh/shell.rb', line 125

def sftp
  ftp_obj = Ghaki::NetSSH::FTP.new( self )
  if block_given?
    yield ftp_obj
  else
    return ftp_obj
  end
end

#telnet(opts = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ghaki/net_ssh/shell.rb', line 134

def telnet opts={}
  tel_obj = Ghaki::NetSSH::Telnet.new( self, opts )
  if block_given?
    begin
      yield tel_obj
    ensure
      tel_obj.close
    end
  else
    return tel_obj
  end
end

#upload!(loc_file, rem_file) ⇒ Object



157
158
159
# File 'lib/ghaki/net_ssh/shell.rb', line 157

def upload! loc_file, rem_file
  sftp.upload! loc_file, rem_file
end