2010年9月3日金曜日

groovyとJNDIで特定のOU配下のinetOrgPersonを検索する

groovyとJNDIで特定のOU配下のinetOrgPersonを検索するには、以下のコードを実行します。

import javax.naming.*
import javax.naming.directory.*

// LDAPサーバに接続
env = new Hashtable()
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory")
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/ou=system")

env.put(Context.SECURITY_AUTHENTICATION, "simple")
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system")
env.put(Context.SECURITY_CREDENTIALS, "secret")

ctx = new InitialDirContext(env)
// 特定のOU配下のinetOrgPersonを検索
sc = new SearchControls()
sc.setSearchScope(SearchControls.SUBTREE_SCOPE)
results = ctx.search("ou=development,ou=users",
"(objectclass=inetOrgPerson)", sc)
while(results.hasMore()){
// 見つかったinetOrgPersonのcommon nameを表示
println "found:" + results.next().attributes.get("cn")
}


動作環境
groovy 1.7.4, JDK6 Update 21, Apache Directory Server 1.5.7

2010年9月2日木曜日

groovyとJNDIでinetOrgPersonを検索する

groovyとJNDIでinetOrgPersonを検索するには、以下のコードを実行します。

import javax.naming.*
import javax.naming.directory.*

// LDAPサーバに接続
env = new Hashtable()
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory")
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/ou=system")

env.put(Context.SECURITY_AUTHENTICATION, "simple")
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system")
env.put(Context.SECURITY_CREDENTIALS, "secret")

ctx = new InitialDirContext(env)
// inetOrgPersonを検索
sc = new SearchControls()
sc.setSearchScope(SearchControls.SUBTREE_SCOPE)
results = ctx.search("", "(objectclass=inetOrgPerson)", sc)
while(results.hasMore()){
// 見つかったinetOrgPersonのcommon nameを表示
println "found:" + results.next().attributes.get("cn")
}


動作環境
groovy 1.7.4, JDK6 Update 21, Apache Directory Server 1.5.7

2010年9月1日水曜日

groovyとJNDIで特定の名前のOUを検索する

groovyとJNDIで特定の名前のOUを検索するには、以下のコードを実行します。

import javax.naming.*
import javax.naming.directory.*

// LDAPサーバに接続
env = new Hashtable()
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory")
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/ou=system")

env.put(Context.SECURITY_AUTHENTICATION, "simple")
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system")
env.put(Context.SECURITY_CREDENTIALS, "secret")

ctx = new InitialDirContext(env)
// 特定の名前のOUを検索
sc = new SearchControls()
sc.setSearchScope(SearchControls.SUBTREE_SCOPE)
results = ctx.search("ou=groups", "(objectclass=organizationalUnit)", sc)
while(results.hasMore()){
println "found:" + results.next().toString()
}


動作環境
groovy 1.7.4, JDK6 Update 21, Apache Directory Server 1.5.7

2010年8月31日火曜日

groovyとApache Commons NetでNTPサーバから時刻を取得する

groovyとApache Commons NetでNTPサーバから時刻を取得するには、以下のコードを実行します。

import org.apache.commons.net.ntp.*

ntpclient = new NTPUDPClient()
ntpclient.open()
// NTPサーバより時刻取得
ti = ntpclient.getTime(InetAddress.getByName("ntp.nict.jp"))
ntpclient.close()
println "time:" + new Date(ti.getReturnTime())



動作環境
groovy 1.7.4, JDK6 Update21, Apache Commons Net 2.0

2010年8月30日月曜日

groovyとJNDIでOUを検索する

groovyとJNDIでOUを検索するには、以下のコードを実行します。

import javax.naming.*
import javax.naming.directory.*

// LDAPサーバに接続
env = new Hashtable()
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory")
env.put(Context.PROVIDER_URL, "ldap://localhost:10389/ou=system")

env.put(Context.SECURITY_AUTHENTICATION, "simple")
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system")
env.put(Context.SECURITY_CREDENTIALS, "secret")

ctx = new InitialDirContext(env)
// OUを検索
sc = new SearchControls()
sc.setSearchScope(SearchControls.SUBTREE_SCOPE)
results = ctx.search("", "(objectclass=organizationalUnit)", sc)
while(results.hasMore()){
println "found:" + results.next().toString()
}


動作環境
groovy 1.7.4, JDK6 Update 21, Apache Directory Server 1.5.7

2010年8月26日木曜日

groovyとJRegistryでデフォルトブラウザの実行ファイルを表示する

groovyとJRegistryでデフォルトブラウザの実行ファイルを表示するには、以下のコードを実行します。

import com.registry.*

// コンピュータのデフォルトブラウザの実行ファイルを表示する
regkey = RegistryKey.parseKey("HKEY_CLASSES_ROOT\\"+
"http\\shell\\open\\command")
println regkey.getValue(RegistryKey.DEFAULT_VALUE_NAME).getValue()


動作環境
groovy 1.7.2, JDK6 Update20, JRegistry 1.6.5

関連項目
JRegistryのdownloadページ
http://sourceforge.net/projects/jregistry/

2010年8月25日水曜日

groovyとJRegistryでWindowsの使用者の組織を表示する

groovyとJRegistryでWindowsの使用者の組織を表示するには、以下のコードを実行します。

import com.registry.*

// コンピュータの使用者の組織を表示する
regkey = RegistryKey.parseKey("HKEY_LOCAL_MACHINE\\"+
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
println regkey.getValue("RegisteredOrganization").getValue()


動作環境
groovy 1.7.2, JDK6 Update20, JRegistry 1.6.5

関連項目
JRegistryのdownloadページ
http://sourceforge.net/projects/jregistry/