Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
executing command

Posted by wei (wei), 25 May 2007

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";
print "<head><title>eg</title></head>\n";
print "<body>";
$command  = `lsof -i`;  #want to get list of open port
print "$command";
print "</body>";


Posted by admin (Graham Ellis), 25 May 2007
You 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 2007
sry a typo error...but even is the variable is the same the command still can't work

Posted by admin (Graham Ellis), 30 May 2007
I 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 2007
if 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 2007
You 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 2007

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.

any ideas?

Posted by admin (Graham Ellis), 9 June 2007
Are 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=}a short item with some source code[/url] about that this morning.

Posted by wei (wei), 10 June 2007
it 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 2007
Note my previous post .... it is

1. Dangerous

2. Inefficient

to do what you're doing.

But, yes, you could link them with a ";" or "\n" just as you would in shell.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho