+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, Python script ,escaped quotes - zimbra account autocreation in Coding and Web Development; I've written the following script in python with python-ldap to pull accounts from Active Directory using ldap. The idea is ...
  1. #1


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,033 Times in 813 Posts
    Rep Power
    341

    Python script ,escaped quotes - zimbra account autocreation

    I've written the following script in python with python-ldap to pull accounts from Active Directory using ldap.
    The idea is that it will run nightly in cron and create any acounts I need on a zimbra mailserver (it can be easily modified to do other stuff - and should run ok on mac/*nix/windows - not for zimbra obviously)
    It all seems to work except for one bit - Zimbra needs me to run the command
    Code:
    zmprov ca username@domain '' displayname name
    the command has 2 single quotes (blank password because I authenticate to AD) and this is where I'm stuck - I can easily escape the single quotes if I'm using the print command eg:
    Code:
    >>> password = "\'\'"
    >>> print password
    ''
    >>>
    but not if I use
    Code:
     os.system('echo command  %s ' % (password))
    ... I gust get blanks for the password and no quotes, I also tried triplequoting

    script
    Code:
    #!/usr/bin/python
    # This file is covered by GNU GPL.
    
    import ldap, string, os
    
    domain = "example.com" 
    l=ldap.initialize("ldap://server.example.com":389") # pick an LDAP server
    l.simple_bind_s("domain\\ldap_username","ldap_password")
    try:
      res = l.search_s('ou=students,dc=example,dc=com',
      ldap.SCOPE_SUBTREE, "(&(ObjectCategory=user) (userAccountControl=512))", ['sAMAccountName','givenName','sn']
    #userAccountControl  512 = normal , 514 = disabled account
      for (dn, vals) in res:
        accountname = vals['sAMAccountName'][0].lower()
        try:
          sirname = vals['sn'][0].lower()
        except:
          sirname = vals['sAMAccountName'][0].lower()
        try:
          givenname = vals['givenName'][0]
        except:
          givenname = vals['sAMAccountName'][0].lower()
    
        initial = givenname[:1].upper()
        sirname = sirname.capitalize()
        name = initial + "." + sirname
        accountname = accountname + "@" + domain
        password = "  \'\' "
        print password
        print accountname,name
        os.system('echo command ca %s %s displayName %s' % (accountname,password,name))
    
    except ldap.LDAPError, error_message:
      print error_message
    
    l.unbind_s()
    Any help appreciated.

    ps - please feel free to improve/use/laugh at this code

  2. #2

    Join Date
    Feb 2006
    Posts
    1,187
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: Python script ,escaped quotes - zimbra account autocreation

    As the password will be blank regardless why don't you just have the quotes in the string e.g.
    Code:
    os.system('echo command ca %s \'\' displayName %s' % (accountname,name))
    You could try insrting actual ASCII for single quote as either hex or octal.
    Then again are you using UNICODE?

  3. #3


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,033 Times in 813 Posts
    Rep Power
    341

    Re: Python script ,escaped quotes - zimbra account autocreat

    thanks networkgeezer - I did try escaping them in the string but it seems that I caused the problem using 'echo' to test the script - when I changed it to run zmprov it worked first time. - It was a dumb mistake but I should have realised that issuing echo with
    Code:
     echo ''
    prints nothing!

    horray - no more manual email account creation !

  4. #4

    Join Date
    Feb 2006
    Posts
    1,187
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: Python script ,escaped quotes - zimbra account autocreation

    Hmmmm .... this is all a bit fishy. You just wanted to show off your Python code

    Why don't you just start a blog on your adventures with Zimbra.

  5. #5


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,033 Times in 813 Posts
    Rep Power
    341

    Re: Python script ,escaped quotes - zimbra account autocreat

    Hmmmm .... this is all a bit fishy. You just wanted to show off your Python code
    lol, it was an honest mistake. I wanted to be dead sure it was working before I started issuing system commands! besides, I've only ever written a couple of other scripts, so its probably not the most elegant.

    Why don't you just start a blog on your adventures with Zimbra.
    Yes. I should. I'm making good progress so far, It would make good documentation for when it all goes titsup :/

  6. #6

    Join Date
    Oct 2008
    Posts
    6
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I believe this is the script that I've been using for my Zimbra system. It's worked perfectly in the past, but we have recently moved across to a new domain and so I have been trying to update the script so that it syncronises with the new AD server.

    However, after changing all of the attributes, when I now try to run the script it returns nothing - no errors or anything, it just starts a new line.

    I'm very confused - it's as if it's connected to the server (if i change the server or usernames it returns errors as it cannot connect) but is just not finding any accounts?! We've moved to a server 2008 domain if this helps.

    Any help would be much appreciated! Below is a copy of the attributes I have changed..


    # Variables can be changed here:
    banned = 'CN=Restricted Users,OU=Logon Groups,OU=Security Groups,OU=Establishment,DC=HPHS,DC=INTERNAL'
    # an OU for banned users
    scope = 'OU=Establishment,DC=HPHS,DC=INTERNAL'
    #the search scope
    domain = "HPHS.internal" # "example.com"
    ldapserver="hps-dc-01"
    #ldap server
    port="389"
    #ldap port (389 default)
    emaildomain="xxx.leics.sch.uk"
    #the email domain
    ldapbinddomain="HPHS"
    #the domain of the ldap bind account
    ldapbind="xxx"
    #the account name of the account to bind to ldap
    ldappassword="xxx"
    #the ldap password
    pathtozmprov="/opt/zimbra/bin/zmprov"



SHARE:
+ Post New Thread

Similar Threads

  1. Zimbra and SMTP
    By binky in forum Wireless Networks
    Replies: 0
    Last Post: 4th January 2008, 02:53 PM
  2. Choosing a distro for Zimbra
    By TechSupp in forum *nix
    Replies: 10
    Last Post: 31st December 2007, 05:16 PM
  3. Zimbra 4.5 released.
    By CyberNerd in forum Educational Software
    Replies: 10
    Last Post: 28th March 2007, 08:51 AM
  4. Python LDAP module version problem
    By CyberNerd in forum Coding
    Replies: 6
    Last Post: 14th December 2006, 02:18 PM
  5. zimbra v4 released
    By CyberNerd in forum Educational Software
    Replies: 1
    Last Post: 31st August 2006, 03:13 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •