Posted by wei (wei), 25 May 2007Hi
I have some problem executing a command in my cgi script. when i execute the script in command line, the command in the script works, however when i execute the script in the browser, everything except the output of the command was shown.
any solution ?
print "Content-type: text/html\n\n";
$command = `lsof -i`; #want to get list of open port
Posted by admin (Graham Ellis), 25 May 2007You apperar to have run a command and saved the results in a variable called $cmd which you never print out. But you print out a variable called $command which is never set. I think you meant to use the same variable name
Editing to add a comment. My statement above looks absurd now that the original poster has gone back and altered his code. It's really helpful to future readers if posters can refrain from later edits. I know some boards have a "no edits after 24 hours" policy enforced .... we don't, but sometimes I wonder!
Posted by wei (wei), 28 May 2007sry a typo error...but even is the variable is the same the command still can't work
Posted by admin (Graham Ellis), 30 May 2007I wonder if the path in the CGI environment is the same as the ordinary use path. What happens if you give the full path to "lsof"? Have you checked the return status variables?
Posted by wei (wei), 6 June 2007if i just use the command lsof it work but if i add "-i" it won't. Is there any other ways to execute the command beside using `` ?
Posted by admin (Graham Ellis), 6 June 2007You could open a pipe to another command and read back from it line by line .... but that's probably not going to get to the bottom of the issue
Posted by wei (wei), 9 June 2007hi
i am able to exec the command by using exec() and making myself as the super user.
But now i have a problem, i have create a script for user to enter command to the server, so when a user enter the command and hit the button 'ok', it will execute the command at the server and display the ouput if there is any.
example... i entered "ls"
and it display the files in the folder where the script is store
then i entered "cd /root" to go to root and no ouput will be shown when the script return
after that i entered "ls" again to view files in root but the result was not the files in root but was the files in the folder where the script is store.
it seem that after cd /root was execute
it went back to the old directories instead of staying at root.
Posted by admin (Graham Ellis), 9 June 2007Are you running each of your exec's commands separately? If so, each will start its own process and start from the current directory.
Beware - it is very dangerous indeed to let your user enter commands that your script will exec. Don't do it!. Your should use Perl's built in functions instead, unless yo are a security guru and know exactly what you are doing. I happened to write [url=http://www.wellho.net/mouth/1225_Perl-functions-for-directory-handling.html}a short item with some source code[/url] about that this morning.
Posted by wei (wei), 10 June 2007it seem that it was running separately, so is there a way to execute the command as a follow up of the previous command? so that for example when i had change dir, i will be able to list the files for that changed directories.
Posted by admin (Graham Ellis), 10 June 2007Note my previous post .... it is
to do what you're doing.
But, yes, you could link them with a ";" or "\n" just as you would in shell.
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho