Get Mail List from LDAP
March 30, 2007 Leave a comment
Although bash is my first scripting language, it seems I am becomming more and more of a python convert. Today I needed to send an email to everyone with an account on my ldap server. Normally, I would have used something like sed and bash. Although I did still end up using bash, I forced myself to write the regular expression part in python.
The idea with languages like python is to make it so that it’s easier to write your own bit of code to do what you want than it would be to find someone else’s code and use it. This code is probably pretty useless to most people. It is not even a little efficient. But who knows, I may help a budding sysadmin who is just starting to write his own scripts. And by blogging it, hopefully I know where to find it for next time.
Here is the script to pull out user’s email addresses, one per line (which is exactly the type of file my mail client will accept to make an email list):
""" mailfind.py This program was written to get all the email addresses from raw input and print them onto the screen """ import re while 1: try: line = raw_input() + 'n' string = re.search(r"(w*.)?w*@w*.w*",line) try: print string.group() except: continue #this should only happen on the last iteration except: break
I ended up just using some trivial bash stuff to do some of the processing, although this would be pretty easy to have built into the python, but it was even easier to just put it in good old familiar and unmanagable bash.
#!/bin/bash slapcat | ./mailfind.py | sort | uniq
Awesome. Now I have my mail addresses to use for my black-market viagra selling business.