Class: Pike::SSH::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/pike/ssh/connection.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cwdObject (readonly)

Current working directory



13
14
15
# File 'lib/pike/ssh/connection.rb', line 13

def cwd
  @cwd
end

.hostObject (readonly)

Host we’re connected with



16
17
18
# File 'lib/pike/ssh/connection.rb', line 16

def host
  @host
end

.sessionObject (readonly)

Net::SSH session



10
11
12
# File 'lib/pike/ssh/connection.rb', line 10

def session
  @session
end

.stateObject (readonly)

Current state of the connection



22
23
24
# File 'lib/pike/ssh/connection.rb', line 22

def state
  @state
end

.sudo_passwordObject

The sudo password for that connection



25
26
27
# File 'lib/pike/ssh/connection.rb', line 25

def sudo_password
  @sudo_password
end

.userObject (readonly)

User which was used to authenticate



19
20
21
# File 'lib/pike/ssh/connection.rb', line 19

def user
  @user
end

Class Method Details

.cd(new_cwd) ⇒ Object

Changes the woring directory



144
145
146
147
148
149
150
151
152
# File 'lib/pike/ssh/connection.rb', line 144

def cd(new_cwd)
  prc = run("cd #{new_cwd}")

  if prc.success?
    @cwd = new_cwd
  else
    Main.error("Can't change workind directory: #{new_cwd} (#{prc.stderr.strip})")
  end
end

.closeObject

Closes the connection



111
112
113
114
115
116
117
118
# File 'lib/pike/ssh/connection.rb', line 111

def close
  if @session
    begin
      @session.close
    rescue
    end
  end
end

.connect(host, user) ⇒ Object

Connects via ssh to target host if not already connected



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pike/ssh/connection.rb', line 32

def connect(host, user)
  @host = host
  @user = user
  @cwd = '~'

  unless @session
    Main.action "Connecting to #{host}" do
      begin
        @session = ::Net::SSH.start(@host, @user)
        wait!
        true
      rescue Exception => e
        Main.error(e.inspect)
      end
    end
  end
end

.open_channel(&method) ⇒ Object

Wrapper for Session.open_channel



159
160
161
# File 'lib/pike/ssh/connection.rb', line 159

def open_channel(&method)
  @session.open_channel &method
end

.ready?Boolean

Checks if the class is ready to run commands

Returns:

  • (Boolean)


134
135
136
# File 'lib/pike/ssh/connection.rb', line 134

def ready?
  @session != nil
end

.run(cmd) ⇒ Object

Executes a command



101
102
103
104
# File 'lib/pike/ssh/connection.rb', line 101

def run(cmd)
  connection_error unless ready?
  Runner.run(self, cmd)
end

.scp(local, remote, host) ⇒ Object

Copies a file via scp to the remote server



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pike/ssh/connection.rb', line 82

def scp(local, remote, host)
  if ready?
    Logger.log "Uploading file #{local} to #{host}:#{remote} ..."

    begin
      @session.scp.upload!(File.expand_path(local), remote)
    rescue Exception => e
      Main.error('Something went wrong with scp.', e)
    end
  else
    connection_error
  end
end

.try_sudoObject

Try’s sudo to ensure the password prompt will not pop again



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pike/ssh/connection.rb', line 55

def try_sudo
  if @session
    Main.action "Trying sudo" do
      result = run("sudo echo 'ok'")

      if result.success?
        if result.stdout.strip.end_with?('ok')
          true
        else
          Main.error('Unexpected data received while trying sudo: ' + result.stderr)
        end
      else
        Main.error('sudo failed. Wrong password?')
      end
    end

    false
  else
    connection_error
  end
end

.wait!Object

Wait method, wrapper for Net::SSH::Session.loopt



125
126
127
# File 'lib/pike/ssh/connection.rb', line 125

def wait!
  @session.loop
end