2

I am newbie to web programming and I have small shell script which basically gives output with two results. It's basically to find users on our directory.

#!/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

Looking out for perl web code which can display the results on browser.

I know, I would be asking too much here, but I would really appreciate if anyone can give me right direction to achieve this.

Thanks!

2
  • where is the code running (on the machine running the webserver)? Commented Sep 26, 2012 at 7:07
  • yes, the code is running on the webserver. Commented Sep 26, 2012 at 11:04

1 Answer 1

1

First of all, do not use $USERNAME in your BASH script. $USERNAME is a BASH variable that contains the current user's name. In fact, it is generally a bad idea to use UPPERCASE variables in BASH. Most BASH environment variables are upper case and that can lead to confusion. It is good practice to have your variables lower case.

Also, since I imagine you want to do this using an HTML form, you cannot have BASH read from STDIN. Modify tour script to take the user name as an argument:

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>

This should do what you need. It assumes that both scripts are in the ./cgi-bin/ directory of your webpage and are called display_name.sh and display_name.pl. It also assumes that you have set their permissions correctly (they need to be executable by apache2's user, www-data). Finally, it assumes that you have set up apache2 to allow execution of scripts in ./cgi-bin.

Is there a specific reason you want to use BASH? You could just do everything directly from the Perl script:

#!/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>";
Sign up to request clarification or add additional context in comments.

7 Comments

Terdon, Many Thanks. I was trying your perl script. I am encountering below error. Global symbol "$USERNAME" requires explicit package name at /var/www/cgi-bin/ldapquery line 13. Any idea?
Please read my answer again @maneeshshetty, I explain this on the very first line. try using the BASH script I gave in my answer.
Sorry for that Terdon. I changed it to $user still it's getting Global symbol "$user" requires explicit package name at /var/www/cgi-bin/ldapquery line 13. Also I tried your Bash code. I am getting syntax error at /var/www/cgi-bin/display_name.pl line 20, near "body>"
Getting error Global symbol "$user" requires explicit package name at /var/www/cgi-bin/ldapquery line 13.
@maneeshshetty, please copy my script as I have posted it. The error you give is quite informative. It tells you that there is a problem with a variable called $user on line 13 of the script. My script has no $user variable on any line. Line 13 has a $name variable. My guess is that you have changed $name to $user on line 13 without the corresponding change for the variable declaration at line 9.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.