Solved

Script to execute external programs

Posted on 2000-02-17
8
167 Views
Last Modified: 2013-12-25
I need some help writing a cgi script that allows a user to select (from check boxes) a list of programs to run and then run those progams (ping, traceroute, etc) and have the output on he same web page.

The page should have a list of check boxes and when they click submit the programs associated get execute. The output for each program should be on the bottom of the page (or even a little popup window)

Thanks
0
Comment
Question by:mdlittle
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2535174
try this................

currently i only have ping & traceroute, but you can add as many as you want.


====commands.pl
#!/usr/local/bin/perl

use CGI;

$query=new CGI;

print "Content-type: text/html\n\n";

print "
<form method=post action=\"".$ENV{SCRIPT_NAME}."\">
<TABLE BORDER=1>
<TD><input type=checkbox name=cb1 value=\"ping\">Ping</TD>
<TD><input type=text name=ping></TD>
<TR>
<TD><input type=checkbox name=cb1 value=\"traceroute\">Traceroute</TD>
<TD><input type=text name=traceroute></TD>
<TR>
<TD align=center colspan=2><input type=submit value=\"Show me!!\"></TD>
</TABLE>
</form>";

if ($query->param){
  print "<hr>\n";
  @cb=$query->param('cb1');
  $command{'ping'}=$query->param('ping');
  $command{'traceroute'}=$query->param('traceroute');
  $command{'ping'}=~ s/\s+/ /g; ##  Squeeze multiple space to one space
  $command{'traceroute'}=~ s/\s+/ /g; ##  Squeeze multiple space to one space

  if (scalar @cb == 0){
    print "<B>Select at least one item!!</B>\n";
    exit;
  }

  foreach $command (@cb){
    if ($command{$command}){  ## Param for that check box specified
      if ($command=~ /^ping$/){
        #$output="$command -c 3 $command{$command} 2> /dev/null";
        $output=`$command -c 3 $command{$command} 2> /dev/null`;
      }else{
        #$output="$command $command{$command} 2> /dev/null";
        $output=`$command $command{$command} 2> /dev/null`;
      }
      print "<B>",$command,"</B><BR>\n";
      print "<pre>",$output,"</pre><P>\n";
    }else{
      print "<B>You need to specify param for $command</B><BR>\n";
    }
  }
}
0
 

Author Comment

by:mdlittle
ID: 2535398
Adjusted points to 550
0
 

Author Comment

by:mdlittle
ID: 2535400
I know I did not ask for this... but is there a way to specifiy which traceroute and which ping program to use (in the script not on the web page) as configuration parameters. I want to do this because I may not use ping or traceroute - it may be other programs.

This is exactly what I wanted and if you could help with the above I will immediately award points. I increased it 50 points

Thanks
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Expert Comment

by:maneshr
ID: 2535585
as per your original question ".....programs to run and then run those progams (ping, traceroute, etc)...." you clearly want to run ping,  traceroute etc....

what do you want do do when you now  say "....but is there a way to specifiy which traceroute and which ping program to use ......" & "I want to do this because I may not use ping or traceroute - it may be other programs. "

do you mean you might want to run your own program, inside which you have ping and /or traceroute commands???

Pl. clarify. if possible provide some sample programs
0
 

Author Comment

by:mdlittle
ID: 2535718
I may want to run other programs other than ping and traceroute. It's pretty simple. You script works as I had requested I just wanted a little more clairification on how to add additional programs.

Mike
0
 
LVL 16

Accepted Solution

by:
maneshr earned 550 total points
ID: 2535764
Ok....

this part of the PERL script print the HTML form(checkbox etc..)

print " 
<form method=post action=\"".$ENV{SCRIPT_NAME}."\">
<TABLE BORDER=1>
<TD><input type=checkbox name=cb1 value=\"ping\">Ping</TD>
<TD><input type=text name=ping></TD>
<TR>
<TD><input type=checkbox name=cb1 value=\"traceroute\">Traceroute</TD>
<TD><input type=text name=traceroute></TD>
<TR>
<TD align=center colspan=2><input type=submit value=\"Show me!!\"></TD>
</TABLE>
</form>";

Now lets say you want to add another program (which), here's how you do it..

1) add ...

<TD><input type=checkbox name=cb1 value=\"which\">Which</TD>
<TD><input type=text name=which></TD>
<TR>

NOTE: the value of the checkbox var is the same as the name of the textbox.

2) in the if statement add this....

if ($query->param){
print "<hr>\n";
@cb=$query->param('cb1');
$command{'ping'}=$query->param('ping');
$command{'traceroute'}=$query->param('traceroute');

$command{'which'}=$query->param('which');  ##Add this

$command{'ping'}=~ s/\s+/ /g; ##  Squeeze multiple space to one space
$command{'traceroute'}=~ s/\s+/ /g; ##  Squeeze multiple space to one space

$command{'which'}=~ s/\s+/ /g; ##   Add this


That's it!!

you are ready to go.

0
 

Author Comment

by:mdlittle
ID: 2535805
Awesome. Thanks for your help - again!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2535821
most welcome!! :-)
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question