Lux*_*pes 6 statistics r ldap ldap-query rcurl
我想针对LDAP目录查询员工在部门和组中的分布情况......
类似于:" 给我一个组的所有成员的部门名称 ",然后使用R进行频率分析,但我找不到任何关于如何使用R连接和运行LDAP查询的示例.
RCurl似乎有某种支持(http://cran.r-project.org/web/packages/RCurl/index.html):
此外,底层实现是健壮和广泛的,支持FTP/FTPS/TFTP(上传和下载),SSL/HTTPS,telnet,dict,ldap,还支持cookie,重定向,身份验证等.
但我不是R的专家,并且无法找到使用RCurl(或任何其他R库)的单个示例来执行此操作.
现在我正在使用这样的CURL来获取一个组的成员:
curl "ldap://ldap.replaceme.com/o=replaceme.com?memberuid?sub?(cn=group-name)"
Run Code Online (Sandbox Code Playgroud)
这里的任何人都知道如何在RC中使用RCurl做同样的事情吗?
自己找到了答案:
首先运行此命令以确保安装RCurl(如http://www.programmingr.com/content/webscraping-using-readlines-and-rcurl/中所述):
install.packages("RCurl", dependencies = TRUE)
library("RCurl")
Run Code Online (Sandbox Code Playgroud)
然后使用ldap URL来访问 getURL(如http://www.ietf.org/rfc/rfc2255.txt中所述,但在我阅读http://docs.oracle.com/cd/E19396之前我无法理解它-01/817-7616/ldurl.html并看到ldap[s]://hostname:port/base_dn?attributes?scope?filter):
getURL("ldap://ldap.replaceme.com/o=replaceme.com?memberuid?sub?(cn=group-name)")
Run Code Online (Sandbox Code Playgroud)
我在这里编写了一个函数来将ldap输出解析为数据帧,并且我使用提供的示例作为参考来获取所有内容.
我希望它对某人有所帮助!
library(RCurl)
library(gtools)
parseldap<-function(url, userpwd=NULL)
{
ldapraw<-getURL(url, userpwd=userpwd)
# seperate by two new lines
ldapraw<-gsub("(DN: .*?)\n", "\\1\n\n", ldapraw)
ldapsplit<-strsplit(ldapraw, "\n\n")
ldapsplit<-unlist(ldapsplit)
# init list and count
mylist<-list()
count<-0
for (ldapline in ldapsplit) {
# if this is the beginning of the entry
if(grepl("^DN:", ldapline)) {
count<-count+1
# after the first
if(count == 2 ) {
df<-data.frame(mylist)
mylist<-list()
}
if(count > 2) {
df<-smartbind(df, mylist)
mylist<-list()
}
mylist["DN"] <-gsub("^DN: ", "", ldapline)
} else {
linesplit<-unlist(strsplit(ldapline, "\n"))
if(length(linesplit) > 1) {
for(line in linesplit) {
linesplit2<-unlist(strsplit(line, "\t"))
linesplit2<-unlist(strsplit(linesplit2[2], ": "))
if(!is.null(unlist(mylist[linesplit2[1]]))) {
x<-strsplit(unlist(mylist[linesplit2[1]]), "|", fixed=TRUE)
x<-append(unlist(x), linesplit2[2])
x<-paste(x, sep="", collapse="|")
mylist[linesplit2[1]] <- x
} else {
mylist[linesplit2[1]] <- linesplit2[2]
}
}
} else {
ldaplinesplit<-unlist(strsplit(ldapline, "\t"))
ldaplinesplit<-unlist(strsplit(ldaplinesplit[2], ": "))
mylist[ldaplinesplit[1]] <- ldaplinesplit[2]
}
}
}
if(count == 1 ) {
df<-data.frame(mylist)
} else {
df<-smartbind(df, mylist)
}
return(df)
}
Run Code Online (Sandbox Code Playgroud)