[Freeipa-users] How can I change my password from a python script?

Joe Linoff jlinoff at tabula.com
Thu Jun 28 15:09:41 UTC 2012


Hi Petr:

Thank you. This is perfect. I have to say that the more I learn about
FreeIPA, the more impressed I am with it (and I was pretty impressed to
begin with).

Regards,

Joe

-----Original Message-----
From: Petr Vobornik [mailto:pvoborni at redhat.com] 
Sent: Thursday, June 28, 2012 1:32 AM
To: Joe Linoff
Cc: freeipa-users at redhat.com
Subject: Re: [Freeipa-users] How can I change my password from a python
script?

On 06/28/2012 03:34 AM, Joe Linoff wrote:
> Hi Everybody:
>
>
>
> I need to add a lot of users to an LDAP system for testing and I would

> like to do it in batch mode. For my small tests have been doing 
> something like this:

A batch command might be useful for this case.

Example (note that I'm not a python guy):

#!/usr/bin/env python

import pprint
from ipalib import api

# Bootstrap
api.bootstrap_with_global_options(context='cli')
api.finalize()
api.Backend.xmlclient.connect()

# Prepare request

users = [
     (u'Foo', u'Bar', u'fbar at foo.baz', u'psw1', u'Sales guy'),
     (u'John', u'Doe', u'jdoe at foo.baz', u'psw2', u'Tech guy'), ]

add_commands = []

for user in users:
     (firstname, surname, email, psw, desc) = user
     add_commands.append({
         "method": 'user_add',
         "params": [
             [],
             {
                 "givenname": firstname,
                 "sn": surname,
                 "mail": email,
                 "userpassword": psw,
                 "setattr": "description='"+desc+"'"
             },
         ],
     })



# Execute as batch
result = api.Command['batch'](*add_commands)

# Print
pp = pprint.PrettyPrinter()
pp.pprint(result)

>
>
>
> #!/bin/bash
>
> # Script to create a new user.
>
> ipa user-add bigbob  \
>
>      --email=bbob at BigBobsEmporium.com \
>
>      --first=Bob \
>
>      --last=Bigg \
>
>      --password  \
>
>      --setattr=description='The sales guy.'<<-EOF
>
> b1gB0bsTmpPwd
>
> b1gB0bsTmpPwd
>
> EOF
>
>
>
> However, I am python guy and would like to use it instead. I am sure 
> that I can do a similar thing using pexpect in python. Probably 
> something like this:
>
>
>
> # This code has not been tested. It is only for a thought experiment.
>
> # Add a user and enter the password using pexpect.
>
> cmd = "ipa user-add bigbob --email='bbob at BigBobsEmporium."
>
> cmd += " --first=Bob --last=Bigg --password "
>
> cmd += "--setattr=description='The sales guy.'"
>
> rets = ['Password', 'Enter Password again to verify', pexpect.EOF, 
> pexpect.TIMEOUT]
>
> c = pexpect.spawn(cmd,timeout=None)
>
> i = c.expect(rets)
>
> if i == 0: # Password
>
>      child.sendline('b1gB0bsTmpPwd')
>
>      i = c.expect(rets)
>
>     if i  == 1: # Enter Password again to verify
>
>          child.sendline('b1gB0bsTmpPwd')
>
>          i = c.expect(rets)
>
>          if  i  == 2:
>
>             print 'SUCCESS'
>
>          else:
>
>              sys.exit('ERROR: something bad happened #1')
>
>      else:
>
>          sys.exit('ERROR: something bad happened #2')
>
> else:
>
>      sys.exit('ERROR: something bad happened #3')
>
>
>
> But I was wondering whether there was a better using the IPA API. Is 
> there a way for me to do that?
>
>
>
> Any help or insights would be greatly appreciated.
>
>
> Thanks,
>
>
>
> Joe
>



--
Petr Vobornik




More information about the Freeipa-users mailing list