Solved

Script to execute external programs

Posted on 2000-02-17
8
153 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now