class Xelnex::Handler

Attributes

connected[RW]
recv_con[RW]
send_con[RW]
timeout[RW]

Public Instance Methods

handle(input) click to toggle source
# File lib/xelnex.rb, line 9
def handle(input)
  if connected
    send_line(input)
  else
    connect!(input)
    send_line("")
  end
end
print(results) click to toggle source

Private Instance Methods

connect!(server) click to toggle source
# File lib/xelnex.rb, line 26
def connect!(server)
  master, slave = PTY.open
  read, write = IO.pipe
  pid = spawn("telnet #{server}", in: read, out: slave)
  read.close
  slave.close

  self.send_con = write
  self.recv_con = master
  self.connected = true
end
send_line(line) click to toggle source
# File lib/xelnex.rb, line 38
def send_line(line)
  send_con.puts line unless line.empty?

  "".tap do |result|
    begin
      loop do
        Timeout::timeout(self.timeout || 0.1) do
          ans = recv_con.gets
          result << ans
        end
      end
    rescue Timeout::Error
    end
  end
end