1

I'm trying to use TCPServer to create both client and server and be able to run server side program. server code:

require 'socket'
STDOUT.sync = true
server = TCPServer.new 8081

loop do
  Thread.start(server.accept) do | client|
    client.puts(%x[ my_external_program  ] )
    client.close
  end
end

client:

require 'socket'
s = TCPSocket.open(my_server, 8081)

while line = s.gets
  puts line.chop
end
s.close

Depending on client request different programs can be executed with differnet parameters. Problem is their execution takes a lot of time and produces really long log files which I'd like to see in real time. Unfortunately this code only prints output to socket when program is finished. I know I could use hudson but really prefer something lighter. Any ideas how to accomplish this?

Marty
  • 31
  • 4
  • 1
    Are you sure you really want to do that? It looks like a huge security hole to me. If you want the user to run external execs you should at least make it run in a chroot. – aledalgrande Aug 07 '15 at 17:10
  • Have you considered to do this with an [asynchronous communication](https://en.wikipedia.org/wiki/Asynchronous_communication)? The server should not be deadlocked by the clients, since it's a security hole (as aledalgrande mentioned) and you specifically want to have real-time information on their progress. – Armfoot Aug 07 '15 at 17:21
  • Normally I'd simply use ssh but my company implemented new firewall recently between some test environments and opened only a couple of ports. Execs are well known, arguments are tested before running, there's also specific xml used. Best practice it is not I agree but we don't really have a lot of options here. – Marty Aug 07 '15 at 17:24
  • @Marty unless the programs the clients execute are able to write logs to someplace where you can track them (i.e. log streams don't get locked by them), I'm not seeing other options... – Armfoot Aug 07 '15 at 17:33
  • They write logs on server side which could be read with i.e. tail -f, but I don't know how to implement it in ruby. – Marty Aug 07 '15 at 18:04
  • 1
    The problem is a little bit more complex than you might think, but it's not unsolvable. You can read this http://stackoverflow.com/questions/22783593/how-to-read-stdout-unbuffered-with-openpopen3-in-ruby and this http://stackoverflow.com/questions/1154846/continuously-read-from-stdout-of-external-process-in-ruby/1162850 to get started. – Casper Aug 07 '15 at 18:11
  • PTY.spawn works perfectly. Thanks! – Marty Aug 07 '15 at 21:38

0 Answers0