简体   繁体   English

调整perl脚本以从cgi运行

[英]Adapt perl script to run from cgi

I have a perl script which works fine from shell but doesn't work from web (lighttpd + mod_cgi). 我有一个perl脚本,该脚本在shell上工作正常,但在web(lighttpd + mod_cgi)下不起作用。 I found out that problem is with the following string 我发现问题在于以下字符串

my $lastupdate = `/opt/mongo/bin/mongo 127.0.0.1:27117/getVersion -u test -p test --eval 'db.polling.find({},{"_id":0,"host":0,"ports":0}).sort({"date":-1}).limit(1).forEach(function(x){printjson(x)})' | awk -F'"' '/date/{print \$4}' |sed 's/T/,/;s/Z//'`;

As i understood, when running from cgi, string is not being splitted. 据我了解,从cgi运行时,字符串不会被拆分。 So i have done this by my own 所以我自己做了

my $lastupdate = system('/opt/mongo/bin/mongo', '127.0.0.1:27117/getVersion', '-u', 'test', '-p', 'test', '--eval', 'db.polling.find({},{"_id":0,"host":0,"ports":0}).sort({"date":-1}).limit(1).forEach(function(x){printjson(x)})', '|', 'awk', '-F', '"', '/date/{print', '\$4}', '|sed', 's/T/,/;s/Z//');

Script works now but gives me unexpected value (differs from shell's run value). 脚本现在可以运行,但是给了我意外的价值(与Shell的运行值不同)。
What did i miss? 我错过了什么?
PS I know that there are smarter ways to interact mongoDB from perl, but my env is totally firewalled. PS我知道有更聪明的方法可以从perl交互mongoDB,但是我的环境完全被防火墙保护了。 I have access neither to CPAN, nor to rh repos and perl mongoDB driver has too much deps to install it manually. 我既无权访问CPAN,也无权访问rh repos,而perl mongoDB驱动程序的部门太多,无法手动安装。

The environment that you run a program under from a shell is completely different to the environment that the same program gets when run from a web server. 从外壳程序运行程序的环境与从Web服务器运行该程序所获得的环境完全不同。 Most obviously, it will be run as a different user - one who will have far more restricted filesystem permissions that the average user. 最明显的是,它将以不同的用户身份运行-该用户具有比普通用户更受限制的文件系统权限。

You can (partly) simulate this by working out which user your web server runs as (perhaps apache, www or nobody) and using sudo to run your program as that user. 您可以(部分地)模拟此情况,方法是确定您的Web服务器以哪个用户身份运行(也许是apache,www或没人),并使用sudo以该用户身份运行程序。 This might well reveal what the problem is. 这很可能揭示问题所在。

You can't just switch from backticks to system() . 您不能只从反引号切换到system() Backticks return the output from running the command line and system() returns a value which requires some interpretation . 反引号返回运行命令行的输出,而system()返回需要解释 That'll be why you're seeing a different result. 这就是为什么您看到不同的结果的原因。

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

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