Solved

Script to execute external programs

Posted on 2000-02-17
8
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

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…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

695 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