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

    Join Date
    Mar 2013
    Rep Power

    Retrieve txt file for input


    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()

    # 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
    print "Enabling account: " + dn
    print "Done."

    # disconnect
    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 God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power

IMN logo majestic logo threadwatch logo seochat tools logo