Snalk

Snalk » OS Tutorials » Unix » Executing Local Shell Commands on the Web

Reply
  #1 (permalink)  
Old 11-03-2008, 10:56 AM
Senior Member
 
Join Date: Aug 2008
Posts: 128
Post Executing Local Shell Commands on the Web

Executing Local Shell Commands on the Web



Consider the following example configuration file:

|item=Show Available Memory|cmd=free
|item=Show Running Processes|cmd=ps auwx
|item=Restart SSH Daemon|cmd=/etc/init.d/sshd restart
|item=Restart Apache Web Server|cmd=/etc/init.d/httpd restart


Each entry has the displayed text (item) and the command to be executed (cmd). This first script only uses the item text to display a list of commands that may be executed:

%# run.mhtml

<%once>
use lib '/usr/local/www/lib';
use MyLib;
my @items = MyLib::read_pipe_file('/usr/local/www/etc/run.conf');
</%once>

<H3>Commands</H3>

% foreach my $entry (@items) {
<P><a href="docmd.mhtml?cmd=<% $entry->{'item'} |u %>">
<% $entry->{'item'} %>
</a></P>
% }


This script will create a simple page with a list of links, as shown in Figure 1. One new feature of Mason is introduced here. The sequence <% … |u %> causes the expression … to be evaluated and its result to be substituted in place of the sequence. The |u portion is the new part. It tells Mason to escape any characters as appropriate for a URL. In this case, any spaces in the item name will be replaced with %20 to create a proper URL. Any other special characters found in the item name will also be properly escaped. For reference, Figure 1 is what the main portion of the page will look like with the given configuration file:



Figure 1: Command execution options


<h3>Commands</h3>

<p><a href="docmd.mhtml?cmd=Show%20Available%20Memory">
Show Available Memory
</a></p>
<p><a href="docmd.mhtml?cmd=Show%20Running%20Processes">
Show Running Processes
</a></p>
<p><a href="docmd.mhtml?cmd=Restart%20SSH%20Daemon">
Restart SSH Daemon
</a></p>
<p><a href="docmd.mhtml?cmd=Restart%20Apache%20Web%20Ser ver">
Restart Apache Web Server
</a></p>


This initial page would be of little use without the page that actually executes the commands. This page is a bit more complicated and is mostly Perl code:

%# docmd.mhtml

<%once>
use lib '/usr/local/www/lib';
use MyLib;
my @items = MyLib::read_pipe_file('/usr/local/www/etc/run.conf');
</%once>

<H3>Executing command "<% $cmd %>"...</H3>
<pre>

<%perl>
foreach my $entry (@items) {
if ($entry->{'item'} eq $cmd) {
unless (open(CMD, "$entry->{'cmd'}|")) {
$m->out("Failed to execute command $entry->{'cmd'}!");
return;
}
while (my $line = <CMD>) {
$m->out("$line");
$m->flush_buffer();
}
close(CMD);
}
}
</%perl>

</pre>
<H3>Done.</H3>

<%args>
$cmd
</%args>

When the user selects a command on the Web, this script looks up that command and executes it. Its output is sent through the Perl filehandle CMD. A filehandle is an identifier used in Perl to read and/or write from and/or to a file—or, in this case, piping output to a command. The Mason function $m->flush_buffer() is called after each line is displayed. This causes the buffer up to that point to be sent to the web browser. This allows the user to see that something is happening if a command takes awhile to run (assuming it produces some output while running). An example of the resulting page can be seen in Figure 2.



Figure 2: Command execution results
Reply With Quote
Reply

Bookmarks

Tags
commands, local shell command, unix, web

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 06:53 AM.
Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.