#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Retrieve txt file for input


    Hi,

    Im new at python but Im looking for a way to retrieve a list of usernames in a text file and then insert them into my script wherever $User would be located.

    I know in powershell it was

    $Users = Get-Content -Path 'C:\temp\NewUser.txt'
    foreach ($user in $users)
    { }


    and this would apply to the entire script is there a way I can do this with python?

    This is what I have for my current python script and it works correctly when trying to only do one username but I would like to be able to have a large list of usernames in a text file and then just play the python script and have it retrieve the usernames


    #! /usr/bin/env python

    import ldap
    import ldap.modlist as modlist
    import getpass

    # ldap url for prod vault
    uri = "ldap://myldap.aaa.aaa.com/"
    l = ldap.initialize(uri)

    print "Connecting to " + uri

    # credentials for LDAP
    user = raw_input("Enter your username: \n")
    username = "cn=" + user + ",ou=Users,o=aaa"
    password = getpass.getpass()

    try:
    # open the connection
    l.bind(username, password)
    print "Connection successful."

    # the dn of our existing entry/object
    uid = input("Enter the userid: \n")
    dn = "cn=" + uid + ",ou=Users,o=aaa"

    # placeholders for old and new values
    old = {'loginDisabled': 'TRUE' }
    new = {'loginDisabled': 'FALSE' }

    # convert place-holders for modify operation using modlist module
    ldif = modlist.modifyModlist(old,new)

    # do the modification
    l.modify(dn,ldif)
    print "Enabling account: " + dn
    print "Done."

    # disconnect
    l.unbind()
    print "Connection closed."

    except ldap.INVALID_CREDENTIALS:
    print "Your username or password is incorrect."

    except ldap.NO_SUCH_OBJECT:
    print dn
    print "Object does not exist."

    except ldap.OPERATIONS_ERROR:
    print "Transaction failed, invalid operation."

    except ldap.OTHER:
    print "Transaction failed, an uknown error occured."

    Thank you for all your help I appreciate it sooooo much!!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481

IMN logo majestic logo threadwatch logo seochat tools logo