Is it possible to run a batch file as a cgi in apa

2019-07-25 12:40发布

问题:

This sounds incredibly simple to do, but I can't make it work. I have it working with awk and perl, but cmd.exe wasn't designed to be a cgi program so no matter what I do, I either get the browser downloading the file, internal server error, or I get the path of the current directory as the first line.

You HAVE to put a #! in the first line of a cgi, or you get an internal server error, but I end up with

C:\Program Files (x86)\Apache Software Foundation\Apache2.2\cgi-bin>#!c:\windows\system32\cmd.exe /c
Content-Type: text/html

<HTML><BODY>
<PRE>Your environment variables are:
</PRE></BODY></HTML>

I've tried with /c, without, any number of permutations, it always prints the current working directory first before anything, so I can't print out the content type as a header.

Anybody ever get this working? I see lots of speculation in the results google shows me, but no actual working examples.

回答1:

  1. First check Apache mine.types, comment out this line .

    #application/x-msdownload exe dll com bat msi

    to

    application/x-msdownload exe dll com bat msi

    This line means : Apache treat .exe, .dll, .com, .batand .msi file as download file instead of executing it. So if you want to run .bat file in cgi , you need to comment it out.(I guess you have already done that, just in case somebody new doesn't know.)

    #AddHandler cgi-script .cgi .bat .ext

    As I tried, if you left this above line commented out, it will not influence.I'm using Apache 2.2 server of Zend . So just leave it alone.

  2. Write your bat file

    #!c:\windows\system32\cmd.exe /c

    This kind of shell-like comment is not working , at the first line of .bat or .cmd file, you need to write :

    @echo off
    
    echo Content-type: text/plain
    
    echo.
    

    The last line means output a new line , if you didn't do that, you will get "Premature end of script headers: xxx.bat " 500 HTTP error.

    echo ^<html^>

    "^" is escape character for cmd.

    echo ....

    The reason why your .bat not working is because that you did not stick to the HTTP protocol.

  3. Put your .bat/.cmd/.exe file into cgi-bin directory.

    Now , check it out.

  4. Advise : read the HTTP protocol.

  5. Other issues:

    If you invoke other .exe in your cgi bat file such as curl.exe ,it will not work as expected,I don't know why , any information will be thanks.

  6. Refers:http://www.jlk.net/apache/debugging_cgi.shtml


回答2:

I'm not 100% clear on the method you are using right now, but...

I don't think you can treat exe files (i.e., cmd.exe) as a CGI binary (e.g., AddHandler cgi-script .exe), nor run .bat files directly.

If you want to execute a batch file via Apache (and display the output on the page), the simplest way to do this is to execute that batch file by running a PHP script that uses exec(path to bat file) to execute the command line operation and echo its output.

Aside from doing this in your Browser via a URL, you can also do this from the command line by using wget/curl, or you can bypass Apache and run the PHP interpreter directly.

And I'm sure you can also do something similar to exec() via perl.