Skip to content

Instantly share code, notes, and snippets.

@ox
Created March 19, 2012 02:27
Show Gist options
  • Save ox/2091114 to your computer and use it in GitHub Desktop.
Save ox/2091114 to your computer and use it in GitHub Desktop.
A sinatra prototype that will run a pty session on the machine and show you a streamed response out to the web.
require 'sinatra'
require 'pty'
get '/' do
"<form action='/' method='POST'><input type='text' name='command'><input type='submit' value='run'></form>"
end
post '/' do
stream do |out|
out << "<html><body><pre>"
begin
PTY.spawn(params['command']) do |r, w, pid|
begin
r.each { |line| out << "#{line}\n" }
rescue Errno::EIO => e
out << "Errno:EIO, #{e.message}"
end
end
rescue Errno::ENOENT => e
out << "Errno::ENOENT, #{e.message}"
rescue PTY::ChildExited
out << "Process complete"
end
out << "</pre></body></html>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment