| 1 |
class SSHConnection
|
| 2 |
def initialize(host)
|
| 3 |
require 'open3'
|
| 4 |
require 'expect'
|
| 5 |
@input = nil
|
| 6 |
@output = nil
|
| 7 |
@error = nil
|
| 8 |
puts 'connecting'
|
| 9 |
#@input, @output, @error = Open3.popen3("setsid ssh "+host+' \'recordio '+$ssh_binpath+' \' 2>log2')
|
| 10 |
@input, @output, @error = Open3.popen3("setsid ssh "+host+' '+$ssh_binpath)
|
| 11 |
#puts 'connected'
|
| 12 |
#puts @output.gets
|
| 13 |
#sleep 5
|
| 14 |
#loop { puts @output.gets }
|
| 15 |
begin
|
| 16 |
@output.expect(/^\*;preauth;time=\d*$/) do
|
| 17 |
puts 'logged in'
|
| 18 |
end
|
| 19 |
|
| 20 |
rescue NoMethodError
|
| 21 |
puts 'Something is borked, make sure sshd is running on selected host'
|
| 22 |
end
|
| 23 |
|
| 24 |
#listenerror
|
| 25 |
end
|
| 26 |
|
| 27 |
def send(data)
|
| 28 |
#puts data
|
| 29 |
begin
|
| 30 |
@input.puts(data)
|
| 31 |
rescue SystemCallError
|
| 32 |
puts 'Write error: '+$!
|
| 33 |
return false
|
| 34 |
rescue IOError
|
| 35 |
puts 'Write error: '+$!
|
| 36 |
return false
|
| 37 |
end
|
| 38 |
return true
|
| 39 |
end
|
| 40 |
|
| 41 |
def listen(object)
|
| 42 |
@listenthread = Thread.new do
|
| 43 |
loop do
|
| 44 |
#begin
|
| 45 |
while line = @output.gets
|
| 46 |
#puts 'o:'+line
|
| 47 |
object.parse_lines(line)
|
| 48 |
end
|
| 49 |
|
| 50 |
#~ rescue IOError
|
| 51 |
#~ puts 'listen: closed stream, disconnecting '+$!
|
| 52 |
#~ close
|
| 53 |
#~ object.disconnect
|
| 54 |
#~ object.connect
|
| 55 |
#~ break
|
| 56 |
#~ rescue StandardError
|
| 57 |
#~ puts 'listen: closed stream, disconnecting '+$!
|
| 58 |
#~ close
|
| 59 |
#~ object.disconnect
|
| 60 |
#~ object.connect
|
| 61 |
#~ break
|
| 62 |
#~ end
|
| 63 |
end
|
| 64 |
end
|
| 65 |
end
|
| 66 |
|
| 67 |
def listenerror
|
| 68 |
@listenthread = Thread.new do
|
| 69 |
f = File.new('log', 'w+')
|
| 70 |
loop do
|
| 71 |
#begin
|
| 72 |
while line = @error.gets
|
| 73 |
f.puts(line)
|
| 74 |
end
|
| 75 |
|
| 76 |
#~ rescue IOError
|
| 77 |
#~ puts 'listen: closed stream, disconnecting '+$!
|
| 78 |
#~ close
|
| 79 |
#~ object.disconnect
|
| 80 |
#~ object.connect
|
| 81 |
#~ break
|
| 82 |
#~ rescue StandardError
|
| 83 |
#~ puts 'listen: closed stream, disconnecting '+$!
|
| 84 |
#~ close
|
| 85 |
#~ object.disconnect
|
| 86 |
#~ object.connect
|
| 87 |
#~ break
|
| 88 |
#~ end
|
| 89 |
end
|
| 90 |
end
|
| 91 |
end
|
| 92 |
|
| 93 |
def close
|
| 94 |
@listenthread.kill if @listenthread
|
| 95 |
@input.close
|
| 96 |
@output.close
|
| 97 |
@error.close
|
| 98 |
end
|
| 99 |
end
|
| 100 |
|
| 101 |
#~ class RbSSHConnection
|
| 102 |
#~ def initialize(host)
|
| 103 |
#~ @input = nil
|
| 104 |
#~ @output = nil
|
| 105 |
#~ @error = nil
|
| 106 |
|
| 107 |
#~ options = {}
|
| 108 |
|
| 109 |
#~ options[:keys] = $ssh_keys if $ssh_keys
|
| 110 |
|
| 111 |
#~ options[:compression] = $ssh_compression if $ssh_compression
|
| 112 |
|
| 113 |
#~ options[:username] = $ssh_username if $ssh_username
|
| 114 |
|
| 115 |
#~ options[:password] = $ssh_password if $ssh_password
|
| 116 |
|
| 117 |
#~ #begin
|
| 118 |
#~ @session = Net::SSH.start(host, options)
|
| 119 |
#~ #rescue StandardError
|
| 120 |
#~ # puts 'error '+$!
|
| 121 |
#~ # return false
|
| 122 |
#~ #end
|
| 123 |
|
| 124 |
#~ @input, @output, @error = @session.process.popen3( $ssh_binpath )
|
| 125 |
#~ sleep 2
|
| 126 |
#~ if @error.data_available?
|
| 127 |
#~ error = 'ERROR: ' + @error.read
|
| 128 |
#~ raise(IOError, error, caller)
|
| 129 |
#~ #return false
|
| 130 |
#~ end
|
| 131 |
|
| 132 |
#~ puts @error.gets
|
| 133 |
|
| 134 |
#~ @sshthread = Thread.new{
|
| 135 |
#~ #Net::SSH.start( host ) do |session|
|
| 136 |
#~ #Thread.current['up'] = true
|
| 137 |
#~ #Thread.current['session'] = session
|
| 138 |
#~ @session.loop
|
| 139 |
#~ #end
|
| 140 |
#~ }
|
| 141 |
#~ #while !@sshthread['up']
|
| 142 |
#~ #stall until thread is up
|
| 143 |
#~ #end
|
| 144 |
#~ puts 'connected via ssh'
|
| 145 |
#~ end
|
| 146 |
|
| 147 |
#~ def send(data)
|
| 148 |
#~ begin
|
| 149 |
#~ @input.puts(data)
|
| 150 |
#~ rescue IOError
|
| 151 |
#~ puts 'closed stream, disconnecting '+$!
|
| 152 |
#~ close
|
| 153 |
#~ return false
|
| 154 |
#~ rescue StandardError
|
| 155 |
#~ puts 'closed stream, disconnecting '+$!
|
| 156 |
#~ close
|
| 157 |
#~ return false
|
| 158 |
#~ end
|
| 159 |
#~ return true
|
| 160 |
#~ end
|
| 161 |
|
| 162 |
#~ def listen(object)
|
| 163 |
#~ @listenthread = Thread.start{
|
| 164 |
#~ while true
|
| 165 |
#~ begin
|
| 166 |
#~ if @output.data_available?
|
| 167 |
#~ #puts 'data'
|
| 168 |
#~ out = @output.read
|
| 169 |
#~ #puts out
|
| 170 |
#~ Thread.start{object.parse_lines(out)}
|
| 171 |
#~ end
|
| 172 |
#~ sleep 1#sleep a little
|
| 173 |
#~ rescue IOError
|
| 174 |
#~ puts 'listen: closed stream, disconnecting '+$!
|
| 175 |
#~ close
|
| 176 |
#~ object.disconnect
|
| 177 |
#~ object.connect
|
| 178 |
#~ break
|
| 179 |
#~ rescue StandardError
|
| 180 |
#~ puts 'listen: closed stream, disconnecting '+$!
|
| 181 |
#~ close
|
| 182 |
#~ object.disconnect
|
| 183 |
#~ object.connect
|
| 184 |
#~ break
|
| 185 |
#~ end
|
| 186 |
#~ end
|
| 187 |
#~ }
|
| 188 |
#~ end
|
| 189 |
|
| 190 |
#~ def close
|
| 191 |
#~ @session.close if @session
|
| 192 |
#~ @sshthread.kill if @sshthread
|
| 193 |
#~ @input = nil
|
| 194 |
#~ @output = nil
|
| 195 |
#~ @error = nil
|
| 196 |
#~ end
|
| 197 |
|
| 198 |
#~ end
|
| 199 |
|
| 200 |
|
| 201 |
class UnixSockConnection
|
| 202 |
def initialize(file)
|
| 203 |
begin
|
| 204 |
@socket = UNIXSocket.open(file)
|
| 205 |
rescue
|
| 206 |
raise(IOError, 'Error: Could not connect to socket '+file, caller)
|
| 207 |
end
|
| 208 |
puts 'connected via unix socket'
|
| 209 |
end
|
| 210 |
|
| 211 |
def send(data)
|
| 212 |
begin
|
| 213 |
@socket.send(data, 0)
|
| 214 |
rescue SystemCallError
|
| 215 |
puts 'Broken Pipe to Irssi, disconnecting '+$!
|
| 216 |
close
|
| 217 |
return false
|
| 218 |
end
|
| 219 |
return true
|
| 220 |
end
|
| 221 |
|
| 222 |
def listen(object)
|
| 223 |
@listenthread = Thread.start{
|
| 224 |
input = ''
|
| 225 |
begin
|
| 226 |
while line = @socket.recv(70)
|
| 227 |
if line.length == 0
|
| 228 |
sleep 1
|
| 229 |
end
|
| 230 |
input += line
|
| 231 |
if input.count("\n") > 0
|
| 232 |
pos = input.rindex("\n")
|
| 233 |
string = input[0, pos]
|
| 234 |
input = input[pos, input.length]
|
| 235 |
Thread.start{
|
| 236 |
object.parse_lines(string)
|
| 237 |
}
|
| 238 |
end
|
| 239 |
end
|
| 240 |
|
| 241 |
rescue SystemCallError
|
| 242 |
puts 'Broken Pipe to Irssi, disconnecting '+$!
|
| 243 |
close
|
| 244 |
end
|
| 245 |
}
|
| 246 |
end
|
| 247 |
|
| 248 |
def close
|
| 249 |
@listenthread.kill
|
| 250 |
@socket.close
|
| 251 |
@client = nil
|
| 252 |
end
|
| 253 |
|
| 254 |
end |