LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2001, 11:38 AM   #1
AliceC
LQ Newbie
 
Registered: Apr 2001
Posts: 4

Rep: Reputation: 0
Question


hi linux programmers

my friend and i are writing a perl program, the idea is using web browser as command prompt. a form for user to enter a command for the remote linux computer, then the output/result will print out in the browser.

However, some of the command still unable to do like "cd" ...etc
what it can do is "ls", "man" ... something like that...

#!/usr/bin/perl

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


sub form_handler
{
my($hashRef) = shift;
my($buffer)="";
if ($ENV{'REQUEST_METHOD'} eq 'GET' ){
$buffer=$ENV{'QUERY_STRING'};
}
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
#print "$buffer";
foreach (split(/&/, $buffer)) {
my($key, $value) = split(/=/, $_);
#print "$key = $value";
$key = decodeURL($key);
$value=decodeURL($value);
%{$hashRef} -> {$key} = $value;
}
}

sub decodeURL {
$_ = shift;
tr/+/ /;
s/%(..)/pack('c', hex($1))/eg;
return($_);
}

sub form{
print "<form action='command.cgi'><input type=text name=command><input
type=submit></form><br><br>";
}


form_handler(\%form);
form();

print $form{command};
if($form{command}){
$temp = `$form{command}`;
$_ = $temp;


#s/\x0d/<br>/eg;
print $_;
}

ANY suggestion(s) for further improvement plz ??thx a lot
 
Old 04-05-2001, 10:09 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Not a bad script. However, the way that you have implemented the script, some commands like 'cd' will never work. cd is not a executable program like ls. It is is a command interpreted by you shell (bash). Try typing 'which cd' on your command line. You will not be able to invoke any shell commands in you script.

Some important things to remember when your script runs are:
  • It does not run in a shell.
  • Most likely runs as the web user nobody (unless you have setuid or suEXEC).
  • Has a limited execution path.
  • Does not persist data. (script exits each time)

To get something like 'cd' to work you will have to:[list=1][*] Capture the input command 'cd' and save the path provided to a file.[*] Next time through the script; first 'chdir( )' to that directory and then execute the command given.[/list=1]

I don't know if things like 'cd ..' will work; heck I don't know if any of this will work, I've never done anything like it before. I don't even know perl either, so you may not want to listen to me.

Good Luck

Gary


ps

A little trick I learned when posting source code is use the 'code tags'. They preserve the indenting and correct spacing.

http://www.linuxquestions.org/questi...?action=bbcode
 
Old 04-06-2001, 12:05 AM   #3
AliceC
LQ Newbie
 
Registered: Apr 2001
Posts: 4

Original Poster
Rep: Reputation: 0
still thinking :D

nice to get feeback in a short time

i guess i know what u trying to say but my friend and i still need some time to think about it

thx a lot gary
 
Old 04-25-2001, 09:05 AM   #4
ratto99
LQ Newbie
 
Registered: Apr 2001
Location: Chicago
Posts: 5

Rep: Reputation: 0
Lightbulb Another Idea

Not being a perl programmer, but alot of programming exp. I would think that if you capture your command from the browser and then run the command though another shell before trying to execute it, you may be able to run it like a command line instead of an input from the browser...

Something like running a variable from your browser into a batch file in dos to execute command line coding on the fly...

PS: newbie to linux, so if this will not work, I would like to know why... still learning.
 
Old 05-17-2001, 10:18 PM   #5
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 47
You really want to consider using the perl CGI module. Then you can do things loke

[CODE}
===========================================================
!#/usr/loca/bin/perl -w

user CGI qw(:standard);
print "Content-type: text/html\nPragma: no-cache\n\n";

$command = param('command');

print("You attempted to execute command $command<BR>");
===========================================================
[/CODE]

That way you don't need to worry about decoding things yourself I would advise you always run perl with -w so you get nice warnings. It will spot things like

Code:
$myvar = 10;
if ($myVar <= 10) {
  #won't be executed! but isn't illegal as $myVar will
  #be created
}
The above would produce a warning along the lines of "warning: possible typo, $myvar only used once, line 1
warning: possible typo, $myVar only used one, line 2"

Although you can't do a 'cd' you can keep track of the logical current directory and prepend this to all commands, for example is the use issues a "cd mydir" you set $path = $path . "mydir/"; then when he does an ls you call "ls $path"

Cheers!

Jamie...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Programming! emailssent Programming 3 06-05-2005 02:25 AM
New to perl programming jester_69 Programming 2 03-17-2005 02:45 PM
Programming in Perl mimf Programming 5 12-04-2003 04:58 AM
Need help with perl (cgi) programming elmetald00d Programming 4 03-28-2002 11:15 PM
Perl programming question LindsayJ Programming 0 11-11-2001 02:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration