LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   awk: loop through ldap aliases (http://www.linuxquestions.org/questions/programming-9/awk-loop-through-ldap-aliases-501503/)

tonedeaf1969 11-14-2006 09:38 AM

awk: loop through ldap aliases
 
Hi All:

Hope you can help out a scripting newbie here. A forum search hasn't given me what I'm looking for. I need to fish out all the alias entries in our LDAP database and match them to the proper user names. I've decided to output a comma delimited file.

If a user has one alias, no problem, that much I've got figured out. It's the users with multiple aliases that have got me stumped. I tried this ham fisted approach(I'm guessing no one has more than 5 aliases):

BEGIN {
UserID = ""
DisplayName = ""
Alias = ""
printf(" UserID,DisplayName,Alias,Alias,Alias,Alias,Alias\n");
}
/^uid: / {UserID=$2}
/^displayName: / {DisplayName=$2" " $3}
/^alias: / {Alias=$2}
/^dn: / {
if(UserID != "") printf("%s,%s,%s,%s,%s,%s,%s\n",UserID,DisplayName,Alias,Alias,Alias,Alias,Alias)
UserID = ""
DisplayName = ""
Alias = ""
Alias = ""
Alias = ""
Alias = ""
Alias = ""
}
END {
printf("%s,%s,%s,%s,%s,%s,%s\n",UserID,DisplayName,Alias,Alias,Alias,Alias,Alias);
}

This prints 5 instances of the first alias, not what I'm looking for. I'm thinking a loop of some kind is what I'm looking for, but I'm not sure how/where to put it. Any pointers would be appreciated. thanks.

fordeck 11-14-2006 09:53 AM

Could you post a sample of your data?

tonedeaf1969 11-14-2006 11:33 AM

Here is my record:

UserID,DisplayName,Alias,Alias,Alias,Alias,Alias
kmmurphy,Kevin Murphy,k.michael.murphy,k.michael.murphy,k.michael.murphy,k.michael.murphy,k.michael.murphy


I use two aliases (k.michael.murphy & kevin.murphy).

FYI: this will run on a SLOX 4.1 mail server w/ BASH

Thanks.

fordeck 11-14-2006 11:52 AM

I was looking for an example of your input data (i.e. 3 or 4 records from your cvs file). The above data looks like your output results.

tonedeaf1969 11-14-2006 01:16 PM

Quote:

Originally Posted by fordeck
I was looking for an example of your input data (i.e. 3 or 4 records from your cvs file). The above data looks like your output results.

OOPS, sorry about that. Here's my ldap ldif entry (I'm using the ldif dump as my data file). Here's the command I'm running.

awk -f test.awk file.ldif > output.csv

dn: uid=kmmurphy,dc=xxxxxxx,dc=xx,dc=xxxx
comFireGroupID: 501
comFireGroupID: 515
comFireGroupID: 516
homeDirectory: /home/kmmurphy
loginShell: /bin/bash
uidNumber: 503
shadowMax: 99999
shadowMin: 0
shadowWarning: 7
shadowInactive: 0
mailenabled: ok
comFireAppointmentDays: 5
comFireTaskDays: 5
objectClass: top
objectClass: shadowAccount
objectClass: posixAccount
objectClass: person
objectClass: inetOrgPerson
objectClass: SuSEeMailObject
objectClass: SuSEcomFireConfig
objectClass: sambaAccount
objectClass: SuSEVirtUserObject
structuralObjectClass: inetOrgPerson
entryUUID: f87e4246-51e8-102a-86ad-ba8d04e51abc
creatorsName: uid=cyrus,dc=xxxxxxx,dc=xx,dc=xxxx
createTimestamp: 20060327142314Z
vaddress: paging@xxxxxxxxx.xxx
vaddress: staff@xxxxxxxxx.xxx
vaddress: support@xxxxxxxxx.xxx
vaddress: it@xxxxxxxxx.xxx
vaddress: log@xxxxxxx.xxx
loginDestination: groupware
gidNumber: 501
DyCEenabled: FALSE
SuSETimeZone: America/Halifax
givenName: Kevin
preferredLanguage: EN
c: CA
sn: Murphy
alias: Kevin.Murphy
alias: k.michael.murphy
o: xxxxxxx xxxxxx xxxxx
userPassword:: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
shadowLastChange: 13381
uid: kmmurphy
pwdLastSet: 1156170155
logonTime: 0
logoffTime: 2147483647
kickoffTime: 2147483647
pwdCanChange: 0
pwdMustChange: 2147483647
displayName: Kevin Murphy
cn: Kevin Murphy
rid: 2006
primaryGroupID: 2003
lmPassword: xxxxxxxxxxxxxxxxxxxxxxx
ntPassword: xxxxxxxxxxxxxxxxxxxxxx
acctFlags: [U ]
mail: Kevin.Murphy@xxxxxxxxx.xxx
OpenLDAPaci: 1#entry#grant;r,w,s,c;cn,initials,title,l,birthday,description,st
reet,postalcode,st,c,susetimezone,homephone,mobile,pager,facsimiletelephonenu
mber,telephonenumber,sn,givenname;r,s,c;[all]#self#
entryCSN: 2006110618:36:10Z#0x0001#0#0000
modifiersName: uid=cyrus,dc=xxxxxxx,dc=xx,dc=xxxx
modifyTimestamp: 20061106183610Z

osvaldomarques 11-16-2006 07:52 PM

Hi,

You have to put the aliases into an array. When it's time to print, you have to print each array element; I got your source and modified it as an example:
Code:

function print_user()
{
  if(UserID != "")
  {
    printf("%s", UserID)
    for (i = 1; i <= ind; i++)
      printf(",%s", Alias[i])
    printf("\n")
  }
}
BEGIN {
UserID = ""
DisplayName = ""
Alias = ""
printf(" UserID,DisplayName,Alias,Alias,Alias,Alias,Alias\n");
}
/^uid: / {UserID=$2}
/^displayName: / {DisplayName=$2" " $3}
/^alias: / {Alias[++ind]=$2}
/^dn: / {
  print_user()
  UserID = ""
  DisplayName = ""
  ind = 0
}
END {
  print_user()
}


tonedeaf1969 11-17-2006 11:56 AM

Quote:

Originally Posted by osvaldomarques
Hi,

You have to put the aliases into an array. When it's time to print, you have to print each array element; I got your source and modified it as an example:
Code:

function print_user()
{
  if(UserID != "")
  {
    printf("%s", UserID)
    for (i = 1; i <= ind; i++)
      printf(",%s", Alias[i])
    printf("\n")
  }
}
BEGIN {
UserID = ""
DisplayName = ""
Alias = ""<======I had to remove this.
printf(" UserID,DisplayName,Alias,Alias,Alias,Alias,Alias\n");
}
/^uid: / {UserID=$2}
/^displayName: / {DisplayName=$2" " $3}
/^alias: / {Alias[++ind]=$2}
/^dn: / {
  print_user()
  UserID = ""
  DisplayName = ""
  ind = 0
}
END {
  print_user()
}


Thanks a lot, that worked fine!
One quick note, I had to remove the "Alias" variable. The script was throwing scalar context errors(see note above).


All times are GMT -5. The time now is 04:17 AM.