[Freeipa-users] KISS: DHCP from IPA

Chris Evich cevich at redhat.com
Thu Aug 30 14:16:32 UTC 2012


On 08/29/2012 03:52 PM, Rob Crittenden wrote:
> Chris Evich wrote:
>> On 08/29/2012 11:57 AM, John Dennis wrote:
>>> Thanks for the contribution Chris!
>>>
>>> Just as an aside if you know Python you can call the IPA commands
>>> directly and use Python to extract and reformat the data, it might be a
>>> lot simpler than doing the bash/awk dance.
>>>
>>
>> I agree that using bash/sed/awk is a bit clunky. I actually did stumble
>> on the python stuff by accident, but wasn't able to find much reference
>> / examples for how to use it. At the time I just needed something quick
>> to toss-together. Maybe the python docs/examples are different today,
>> any links handy?
>>
>
> I seem to recall this came up on either freeipa-users or freeipa-devel
> but I can't find the thread. Some decent examples got posted.
>
> Here is something I've been twiddling with to add users from a
> well-formatted passwd file:
>
> import sys
> import re
> from ipalib import api
> from ipalib import errors
>
> filename='passwd'
> name_pattern = re.compile('(\w+) \w (\w+)')
>
> api.bootstrap(context='cli')
> api.finalize()
> api.Backend.xmlclient.connect()
>
> count = 0
> fd = open(filename, 'r')
> while True:
> line = fd.readline()
> if not line:
> break
> line = unicode(line.strip())
> try:
> (login, passwd, uid, gid, gecos, dir, shell) = line.split(':')
> except ValueError, e:
> print "mal-formed passwd entry: %s (%s)" % (e, line)
> continue
> m = name_pattern.match(gecos)
> if m:
> first = m.group(1)
> last = m.group(2)
> else:
> first = u'USER'
> last = u'NAME'
>
> try:
> api.Command['user_add'](login, gidnumber=int(gid),
> uidnumber=int(uid),
> gecos=gecos.strip(), homedir=dir, shell=shell,
> givenname=first, sn=last)
> except errors.DuplicateEntry:
> print "%s already exists" % login
> continue
> ...
>
> rob

Thanks! That helps.  Still, one can only get so far by reading 
docstrings :)  More examples like this on the wiki, or (even better) 
some API docs would be great!

-- 
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214




More information about the Freeipa-users mailing list