Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help running a command from a form result 1

Status
Not open for further replies.

CarlosBX

Technical User
Joined
Aug 20, 2005
Messages
4
Location
MX
Hi all, this is my firts message here, ok, my problem is this:

I use a program called "myprogram" to remove users in shell form a black list, the way i use it is typing in shell ./myprogram remove USERNAME

I want to use a form to make it not needing to go in shell everytime i want to remove some user from there, and here starts my problem, i can show the result in text without problem, but i cant make the form execute that command, this is my form code:

form.pl file
Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<FORM METHOD='GET' ACTION='result.pl'>";
print "User to remove: <INPUT TYPE='text' NAME='name'>";
print "<INPUT TYPE='SUBMIT' VALUE='REMOVE'>";
print "</FORM>";
and the result shows ok with this code:

result.pl file
Code:
#!/usr/bin/perl

$my_input = $ENV{QUERY_STRING};
##read(STDIN, $my_input, $ENV{CONTENT_LENGTH});
@fv_pairs = split /\&/ , $my_input;

foreach $pair (@fv_pairs) {
   if($pair=~m/([^=]+)=(.*)/) {
      $field = $1;
      $value = $2;
      $value =~ s/\+/ /g;
      $value =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
      $INPUT{$field}=$value;
   }
}

print "Content-type: text/plain\n\n";
print "./myprogram remove $INPUT{name}";
But i'm not sure how to make it execute the command, i try changing the last 2 lines to:
Code:
system("/path/to/my/program/myprogram remove $INPUT{name}");

but is not working, i'm learning and i hope someone can help me.

Thank you in advanced and sorry for my bad english
Carlos
 
I think you have to quote the arguments to system() separately:

system("/path/to/my/program/myprogram", "remove", "$INPUT{name}");

try that and see if it works, if not, someone else will come along and help you.
 
Hi again, thank you for the help KevinADC, i try that and still not working, when i tested it with the text output i got now:

Code:
/path/to/my/program/myprogram remove TesterLocation: [URL unfurl="true"]http://myaccount.com/cgi-bin/[/URL]

so i think that is the problem because is adding more text in the command, did you know a way to remove that extra text "Location:
Thank you in advanced.

Carlos
 
Code:
use CGI qw/ :standard /;

my $prog = "/path/to/my/program/myprogram";

my $msg = ''; # error messages from your command

if ( my $victim = param('vic') ) {
   # you should check that $victim is valid
   my $cmd_output = `$prog remove $victim 2>&1`;
   $msg = p("Removing $victim: $cmd_output");
}

print header, start_html( -title=>'Buffy' ),
    h1( 'The Slayer' ),
    $msg,
    start_form,
    p( 'Slay Whom? ', textfield( -name=>'vic' ) ),
    submit,
    end_form,
    end_html;

Keeping it all in one file makes debugging easer and helps with version control. Using CGI.pm early saves you from having to rewrite the whole thing later on when you find you need it (and you will). Trapping output and error from myprogram helps you see what's gone on.

Yours,

fish

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Thank you fishiface, all is working now!

Carlos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top