Solved

Script to execute external programs

Posted on 2000-02-17
8
147 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

708 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

17 Experts available now in Live!

Get 1:1 Help Now