Topic: Create a RailsShell Application in 2 minutes & 10 steps

Create a RailsShell Application in 2 minutes & 10 steps

If you want to be able to execute commands from the command line but you just have a web browser here is a way to do it !!!.

This Application is based in my previous post:
Create a Rails System Information with 1 controller - phpsysinfo like.
http://railsforum.com/viewtopic.php?id=16197

DISCLAIMER: WARNING WARNING WARNING. If you decide to implement this in your server make sure you have some kind of security or restriction to access the page since this could represent a very big security issue.

Lets start with pretty basic stuff to create a Rails Application:

1.- rails shell -d mysql
2.- cd shell
3.- Edit config/database.yml
4.- rake db:create
5.- ruby script\generate scaffold Command execute:string
6.- rake db:migrate
7.- Delete public/index.html
8.- Edit config/routes.rb
     map.resources :commands
9.- Edit the app/controllers/commands_controller.rb and add the @command_result variable in the show method.   

  def show
    @command = Command.find(params[:id])
    @command_result = %x[#{@command.execute.to_s}]
...

This is where we retrieve the @command.execute previously saved and execute it in the Rails server.

10.- Update the app/views/commands/show.html.erb

<p>
  <b>Execute:</b>
  <%=h @command.execute %>
</p>

<pre>
  <%=h @command_result %>
</pre>


<%= link_to 'Edit', edit_command_path(@command) %> |
<%= link_to 'Back', commands_path %>


That's All Folks here is a very simple but powerful way to have an interactive shell of your Rails Server, saving the commands in your applications and execute it every time you click on the show action, don't wait any longer start your web server and think about secure the application !!!.

WARNING: Please use it wisely, since this could represent a BIG security hole in any webserver since let execute commands from the command line using the permissions granted for the user running the Rails Web Server !!!.


Dinooz
Brains R Like Books only work when they R Open

Last edited by dinooz (2008-04-22 17:04:34)

Re: Create a RailsShell Application in 2 minutes & 10 steps

Thanks for the tutorial! smile