When I execute the code below with cn set to the $adgroup variable, I get the following error:
Invalid DN syntax (34)
Additional information: 0000208F: NameErr: DSID-031001F7, problem 2006 (BAD_NAME), data 8349, best match of:
,ou=Resource,ou=groups,dc=abc,dc=somecompany,dc=com'
If I set cn equal to the group instead of using a variable, the code works perfectly fine.
What am I missing in regard to ldapsearch and variables that is causing this error?
#!/bin/bash
#reads Groups.txt file and runs a new ldapsearch for group
while read -r adgroup; do
echo $adgroup
#ldapsearch connects to ldap, returns and formats member info, and writes to input.txt file
ldapsearch -x -LLL -E pr=200/noprompt -o ldif-wrap=no -h abc-loc.somecompany.com \
-D "account@somecompany.com" -w password -b "cn=$adgroup,ou=Resource,ou=groups,dc=abc,dc=somecompany,dc=com" \
| grep member | sed 's/\\/\\\\/g' > /userid/bin/input.txt
#reads input.txt file and runs a new ldapsearch for each member entry
while IFS=":" read -r member info; do
#echo $info
ldapsearch -x -LLL -E pr=200/noprompt -h abc-loc.somecompany.com \
-D "account@somecompany.com" -w password -b "ou=End Users,ou=Accounts,dc=abc,dc=somecompany,dc=com" \
-s sub "distinguishedName=$info" displayName saMAccountName telephoneNumber mail department departmentNumber \
extensionAttribute1 l st co userAccountControl |
#display output string in correct format
awk -F: '
NR == 1 {n = split (FLDS, T)
}
{OUT[$1] = substr ($2, 2)
}
END {for (i=1; i<=n; i++) {printf "%s%s", ODL, OUT[T]
ODL = "|"
}
printf RS
}
' FLDS="displayName:sAMAccountName:telephoneNumber:mail:extensionAttribute1:department:departmentNumber:l:st:co:userAccountControl"
#Define input filepath
done < /userid/bin/input.txt
#Define Group filepath
done < /userid/bin/GroupsTest1.txt
Please show us the content of the input file, wrapped in CODE tags for clarity. There may be something illegal in there, or perhaps your script has to deal with spaces in a different way.
The GroupsTest1.txt file has one line. It is a group that I am wanting to get information from LDAP on. At some point, I'll add more groups to this file. It looks something like this.
ab_abc
The problem seems to be with this portion of code.
Did you make sure the "GroupsTest1.txt" is a true *nix file, NOT an MS file with DOS line terminators <CR> (0x0d, \r, <carriage return>)? How did you create it?
If assigning adgroup manually on the command line, would the ldapsearch command run correctly?
I created both .txt files using VI and yes, the search works perfectly fine if I enter the adgroup manually.
I created a new script in an attempt to isolate the problem area and have discovered that the "while read..." portion of the script seems to be causing the issue. If I take it out and modify the script slightly to build a portion of my ldapsearch string as shown below, I get good results in my input.txt file.
It seems that maybe I'm using "while read" incorrectly as it cannot read and set my adgroup variable in a way that any function within the while read loop can interprate it.