Journal Zarf's Journal: Groovy and the shell
#!/usr/bin/env groovy
println "Hello World!"
Groovy is a neat language you can do curries and functional programming, typing is optional, and you can use it with Grails to generate up web apps really quick. The nifty thing is you can learn one set of API and carry them around with you. You don't have to relearn a bunch of new ways of doing things if you don't want to... or you can if you do want to. Groovy is neat stuff. Grails is getting good. Or you can just use Groovy as a way to dink around with Java API. For example if you have Java and Groovy that means you can play around with ldap from Groovy using the Java API like this:
#!/usr/bin/env groovy
import java.util.Hashtable
import javax.naming.*
import javax.naming.event.*
import javax.naming.directory.*
def ou = " System and Service Accounts"
// here, we're just reading in some parameters...
def stdin = new BufferedReader(
new InputStreamReader(System.in)
)
print "username: "// your username to access LDAP... not a real user
String username = stdin.readLine()
print "password: "// the password for that account...
String passwd = stdin.readLine()
// next we set up an environment for LDAP
Hashtable env = new Hashtable()
env.put(
Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory"
)
env.put(
Context.SECURITY_AUTHENTICATION,
"EXTERNAL"
)
/*// if only I had SSL working... *sigh* oh well...
env.put(
Context.SECURITY_PROTOCOL,
"ssl"
)//*/
// You'll want to put your server's name or IP here:
env.put(
Context.PROVIDER_URL,
"ldap://activedirectoryhost.mycompany.com:636/"
)
// Here, you'll want to set up your company's parameters for OU and DC...
env.put(
Context.SECURITY_PRINCIPAL,
"CN=" + username + ",OU=" + ou + ",DC=mycompany,DC=com"
)// username
env.put(
Context.SECURITY_CREDENTIALS,
passwd
)// and password
// Now we'll try and connect...
try {
def ctx = new InitialDirContext(env)
def attr = ctx.getAttributes("")
def srchInfo = new SearchControls()// you'll want to use your own search base
def searchBase= "OU=company,DC=mycompany,DC=com"// this is the magic search string sauce:
def searchFilter = "(&(objectClass=person))"// we strong type the next var because it is // used in a Java API that needs String[]
String[] objAttribs=[
"givenName",
"cn",
"sn",
"mail",
"mailNickname",
"userPrincipalName",
"displayName",
"manager",
"department",
"memberOf"
]
srchInfo.setSearchScope(SearchControls.SUBTREE_SCOPE)
srchInfo.setReturningAttributes(objAttribs)
// Now we get the results and loop through them...
NamingEnumeration dirObjects = ctx.search(searchBase,searchFilter,srchInfo)
def nodirObjects = 0
while (dirObjects != null && dirObjects.hasMoreElements()) {
def dirObject = dirObjects.next()
println("'" + dirObject.getName() + "':")
def attrs = dirObject.getAttributes()
for(name in objAttribs) {
println "\t * " + attrs.get(name)
}
nodirObjects++
}
ctx.close()
println("Number of entries identified: " + nodirObjects)
} catch (Exception e) {
println "Exception: " + e
}
Groovy and the shell More Login
Groovy and the shell
Slashdot Top Deals