简体   繁体   English

是否可以在Windows下的apache中将批处理文件作为cgi运行?

[英]Is it possible to run a batch file as a cgi in apache under windows?

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. 我可以将其与awk和perl一起使用,但是cmd.exe并非旨在作为cgi程序,因此无论我做什么,我要么会得到浏览器下载文件,内部服务器错误,要么得到当前路径。目录作为第一行。

You HAVE to put a #! 您必须输入#! in the first line of a cgi, or you get an internal server error, but I end up with 在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>

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. 我尝试使用/ c,不带任何数量的排列,它总是先打印当前工作目录,然后再打印任何内容,因此我无法将内容类型打印为标题。

Anybody ever get this working? 有人能做到这一点吗? I see lots of speculation in the results google shows me, but no actual working examples. 我在Google向我展示的结果中看到了很多猜测,但没有实际的工作示例。

  1. First check Apache mine.types, comment out this line . 首先检查Apache mine.types,注释掉这一行。

    #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 , .bat and .msi file as download file instead of executing it. 这一行的意思是:Apache将.exe.dll.com.bat.msi文件视为下载文件,而不是执行该文件。 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.) 因此,如果您想在cgi中运行.bat文件,则需要将其注释掉(我想您已经这样做了,以防万一新人不知道)。

    #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 . 当我尝试过时,如果您将上面的这一行注释掉了,那将不会产生影响。我正在使用Zend的Apache 2.2服务器。 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 : 这种类似shell的注释不起作用,在.bat.cmd文件的第一行,您需要编写:

     @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. 最后一行表示输出新行,如果不这样做,则会得到“脚本头过早结束:xxx.bat” 500 HTTP错误。

    echo ^<html^>

    "^" is escape character for cmd. “ ^”是cmd的转义字符。

    echo ....

    The reason why your .bat not working is because that you did not stick to the HTTP protocol. .bat无法正常工作的原因是,您未遵循HTTP协议。

  3. Put your .bat / .cmd / .exe file into cgi-bin directory. 将您的.bat / .cmd / .exe文件放入cgi-bin目录。

    Now , check it out. 现在,检查一下。

  4. Advise : read the HTTP protocol. 忠告:阅读HTTP协议。

  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. 如果您在cgi bat文件中调用其他.exe (例如curl.exe) ,它将无法按预期运行,我不知道为什么,感谢您提供任何信息。

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

I'm not 100% clear on the method you are using right now, but... 我目前尚不清楚您正在使用的方法,但...

I don't think you can treat exe files (ie, cmd.exe) as a CGI binary (eg, AddHandler cgi-script .exe ), nor run .bat files directly. 我认为您不能将exe文件(即cmd.exe)视为CGI二进制文件(例如AddHandler cgi-script .exe ),也不能直接运行.bat文件。

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. 如果要通过Apache执行批处理文件(并在页面上显示输出),最简单的方法是通过运行使用exec(path to bat file)的PHP脚本来执行该批处理文件来执行命令行操作并回显其输出。

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. 除了通过URL在浏览器中执行此操作外,还可以使用wget / curl在命令行中执行此操作,或者可以绕过Apache并直接运行PHP解释器。

And I'm sure you can also do something similar to exec() via perl. 而且我相信您也可以通过perl exec()类似于exec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM