2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2010-02-23 07:12:54 +00:00
|
|
|
require 'rex'
|
|
|
|
|
|
|
|
module Net
|
|
|
|
module SSH
|
|
|
|
|
|
|
|
class CommandStream
|
|
|
|
|
|
|
|
attr_accessor :channel, :thread, :error, :ssh
|
|
|
|
attr_accessor :lsock, :rsock, :monitor
|
|
|
|
|
|
|
|
module PeerInfo
|
|
|
|
include ::Rex::IO::Stream
|
|
|
|
attr_accessor :peerinfo
|
|
|
|
attr_accessor :localinfo
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(ssh, cmd, cleanup = false)
|
|
|
|
|
|
|
|
self.lsock, self.rsock = Rex::Socket.tcp_socket_pair()
|
|
|
|
self.lsock.extend(Rex::IO::Stream)
|
|
|
|
self.lsock.extend(PeerInfo)
|
|
|
|
self.rsock.extend(Rex::IO::Stream)
|
|
|
|
|
|
|
|
self.ssh = ssh
|
|
|
|
self.thread = Thread.new(ssh,cmd,cleanup) do |rssh,rcmd,rcleanup|
|
|
|
|
|
|
|
|
begin
|
2010-10-07 00:12:12 +00:00
|
|
|
info = rssh.transport.socket.getpeername
|
|
|
|
self.lsock.peerinfo = "#{info[1]}:#{info[2]}"
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
info = rssh.transport.socket.getsockname
|
|
|
|
self.lsock.localinfo = "#{info[1]}:#{info[2]}"
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
rssh.open_channel do |rch|
|
|
|
|
rch.exec(rcmd) do |c, success|
|
|
|
|
raise "could not execute command: #{rcmd.inspect}" unless success
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
c[:data] = ''
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
c.on_eof do
|
|
|
|
self.rsock.close rescue nil
|
|
|
|
self.ssh.close rescue nil
|
|
|
|
self.thread.kill
|
|
|
|
end
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
c.on_close do
|
|
|
|
self.rsock.close rescue nil
|
|
|
|
self.ssh.close rescue nil
|
|
|
|
self.thread.kill
|
|
|
|
end
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
c.on_data do |ch,data|
|
|
|
|
self.rsock.write(data)
|
|
|
|
end
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
c.on_extended_data do |ch, ctype, data|
|
|
|
|
self.rsock.write(data)
|
|
|
|
end
|
2010-02-23 07:12:54 +00:00
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
self.channel = c
|
2010-02-23 07:12:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
self.monitor = Thread.new do
|
|
|
|
while(true)
|
|
|
|
next if not self.rsock.has_read_data?(1.0)
|
2011-05-02 00:23:42 +00:00
|
|
|
buff = self.rsock.read(16384)
|
2010-10-07 00:12:12 +00:00
|
|
|
break if not buff
|
|
|
|
verify_channel
|
|
|
|
self.channel.send_data(buff) if buff
|
|
|
|
end
|
2010-02-23 07:12:54 +00:00
|
|
|
end
|
|
|
|
|
2010-10-07 00:12:12 +00:00
|
|
|
while true
|
|
|
|
rssh.process(0.5) { true }
|
|
|
|
end
|
2010-02-23 07:12:54 +00:00
|
|
|
|
|
|
|
rescue ::Exception => e
|
|
|
|
self.error = e
|
2010-10-07 00:12:12 +00:00
|
|
|
#::Kernel.warn "BOO: #{e.inspect}"
|
|
|
|
#::Kernel.warn e.backtrace.join("\n")
|
2010-02-23 07:12:54 +00:00
|
|
|
ensure
|
|
|
|
self.monitor.kill if self.monitor
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shut down the SSH session if requested
|
|
|
|
if(rcleanup)
|
|
|
|
rssh.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Prevent a race condition
|
|
|
|
#
|
|
|
|
def verify_channel
|
|
|
|
while ! self.channel
|
|
|
|
raise EOFError if ! self.thread.alive?
|
2010-05-20 20:42:17 +00:00
|
|
|
::IO.select(nil, nil, nil, 0.10)
|
2010-02-23 07:12:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|