2012-09-26 41 views
2

我是网络编程的新手,我有一个基本上给出两个结果输出的小shell脚本。基本上是在我们的目录中找到用户。网页从shell脚本中输入并显示结果

#!/bin/bash 
echo -n "Please enter username to lookup: " 
read USERNAME 
DISPLAYNAME=`ldapsearch -p xxx -LLL -x -w test -h abc.com -D abc -b dc=abc,dc=com sAMAccountName=$USERNAME | grep displayName` 

if [ -z "$DISPLAYNAME" ]; then 
    echo "No entry found for $USERNAME" 
else 
    echo "Entry found for $USERNAME" 
fi 

寻找可以在浏览器上显示结果的perl网页代码。

我知道,我会在这里问太多,但我真的很感激,如果任何人都可以给我正确的方向来实现这一目标。

谢谢!

+0

其中代码正在运行(在运行Web服务器的计算机上)? – akira

+0

是的,代码在网络服务器上运行。 – 2012-09-26 11:04:17

回答

1

首先,不是在BASH脚本中使用$USERNAME$USERNAME是一个包含当前用户名的BASH变量。实际上,在BASH中使用UPPERCASE变量通常是一个糟糕的主意。大多数BASH环境变量都是大写字母,可能会导致混淆。让你的变量小写是个好习惯。

此外,因为我想你想要使用HTML表单来做到这一点,所以你不能让BASH从STDIN中读取数据。修改游脚本以将用户名作为参数:

BASH:

#!/bin/bash 
user=$1; 
DISPLAYNAME=`ldapsearch -p xxx -LLL -x -w test -h abc.com -D abc -b dc=abc,dc=com sAMAccountName=$user | grep displayName` 
if [ -z "$DISPLAYNAME" ]; then 
    echo "No entry found for $user" 
else 
    echo "Entry found for $user" 
fi 

的Perl:

#!/usr/bin/perl 
use CGI qw(:standard); 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 
use strict; 
use warnings; 
## Create a new CGI object 
my $cgi = new CGI; 
## Collect the value of 'user_name' submitted by the webpage 
my $name=$cgi->param('user_name'); 

## Run a system command, your display_name.sh, 
## and save the result in $result 
my $result=`./display_name.sh $name`; 

## Print the HTML header 
print header; 
## Print the result 
print "$result<BR>"; 

HTML:

<html> 
<body> 
<form ACTION="./cgi-bin/display_name.pl" METHOD="post"> 
<INPUT TYPE="submit" VALUE="Submit"></a> 
<INPUT TYPE="text" NAME="user_name"></a> 
</form> 
</body> 
</html> 

这应该做你所需要的。它假设这两个脚本都位于网页的./cgi-bin/目录中,并被称为display_name.sh和display_name.pl。它还假定你已经正确设置了他们的权限(他们需要由apache2的用户www-data执行)。最后,它假定您已经设置了apache2来允许执行./cgi-bin中的脚本。

是否有您想使用BASH的特定原因?您可以直接从Perl脚本执行所有操作:

#!/usr/bin/perl 
use CGI qw(:standard); 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 
use strict; 
use warnings; 
## Create a new CGI object 
my $cgi = new CGI; 
## Collect the value of 'name' submitted by the webpage 
my $name=$cgi->param('user_name'); 

## Run the ldapsearch system command 
## and save the result in $result 
my $result=`ldapsearch -p xxx -LLL -x -w test -h abc.com -D abc -b dc=abc,dc=com sAMAccountName=$name | grep displayName`; 

## Print the HTML header 
print header; 
## Print the result 
$result ? 
     print "Entry found for $name<BR>" : 
     print "No entry found for $name<BR>"; 
+0

特尔顿,非常感谢。 我正在尝试你的perl脚本。我遇到下面的错误。 全局符号“$ USERNAME”需要在/ var/www/cgi-bin/ldapquery第13行显式包名。 任何想法? – 2012-09-26 14:51:36

+0

请再次阅读我的答案@maneeshshetty,我在第一行解释这一点。尝试使用我在答案中给出的BASH脚本。 – terdon

+0

对不起,特尔顿。我改变它为$ user仍然是 全局符号“$ user”需要在/ var/www/cgi-bin/ldapquery第13行显式包名称。我得到 语法错误在/var/www/cgi-bin/display_name.pl第20行,在“body>”附近 – 2012-09-26 15:21:44