是否有可能运行一个批处理文件窗口下的Apache一个cgi?(Is it possible to r

2019-10-20 17:37发布

这听起来非常简单的事,但我不能使它发挥作用。 我有它使用awk和perl的工作,但CMD.EXE并没有设计成一个CGI程序所以无论我做什么,我要么得到浏览器下载文件,内部服务器错误,或者我得到当前的路径目录作为第一行。

你必须把一个#! 在CGI的第一行,或者你会得到一个内部服务器错误,但我结束了

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>

我试着/ C,没有,任何数量的排列,它总是显示当前的工作目录第一任何东西之前,所以我不能打印出来的内容类型作为标题。

有人曾经得到这个工作? 我看到很多在谷歌结果显示炒我,但没有实际工作的例子。

Answer 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


Answer 2:

我不是你使用的是现在的方法明确100%,但...

我不认为你可以把exe文件(即CMD.EXE)作为一个CGI程序(例如, AddHandler cgi-script .exe ),也不直接运行.bat文件。

如果要执行通过Apache批处理文件(并显示在页面上输出),要做到这一点最简单的方法是通过运行使用PHP脚本来执行该批处理文件exec(path to bat file)执行命令行操作和回声其输出。

除了通过URL在浏览器中做这个,你也可以通过使用wget /卷曲做到这一点的命令行,或者你可以绕过Apache和直接运行PHP解释器。

我敢肯定,你也可以做类似的东西exec()通过perl的。



文章来源: Is it possible to run a batch file as a cgi in apache under windows?