Re: Handling output from ldapsearch
- From: Glenn Jackman <glennj@xxxxxx>
- Date: 30 Jun 2008 15:07:33 GMT
At 2008-06-30 09:38AM, "utbloke@xxxxxxxxx" wrote:
In the following example output (from an exec call to ldapsearch) am
trying to figure out how to just derive a list of the sAMAccountName
values:
"dn: CN=user01,CN=Users,DC=mynet,DC=local
sAMAccountName: user01
dn: CN=user02,CN=Users,DC=mynet,DC=local
sAMAccountName: user02
dn: CN=user03,CN=Users,DC=mynet,DC=local
sAMAccountName: user03"
...ideally, resulting in a list like this (based on above):
"user01 user02 user03"
The simple way is:
set accts [list]
foreach line [split $output \n] {
lassign [split $line :] key value
if {$key eq "sAMAccountName"} {
lappend accts [string trim $value]
}
}
More generally, I'd write:
package require textutil
set accts [dict create]
set user [dict create]
set key_field sAMAccountName
foreach line [split $out \n] {
if {$line eq ""} {
dict set accts [dict get $user $key_field] $user
set user [dict create]
} else {
dict set user {*}[textutil::splitx $line ": "]
}
}
Now you can get the list of usernames:
set userlist [dict keys $accts]
And you have at your fingertips all the other LDAP data you pulled out,
for example:
dict for {user subdict} $accts {
puts "$user: [dict get $subdict cn]"
}
Use the -LLL flag on your ldapsearch command. I find it's output
easier to parse.
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
.
- References:
- Handling output from ldapsearch
- From: utbloke
- Handling output from ldapsearch
- Prev by Date: Re: Limiting resources used by scripts on web server
- Next by Date: Re: About Tcl syntax...
- Previous by thread: Re: Handling output from ldapsearch
- Next by thread: clock format does not run in window service
- Index(es):