【问题标题】:Convert LDAP search results into CSV将 LDAP 搜索结果转换为 CSV
【发布时间】:2019-08-19 17:18:24
【问题描述】:

我目前正在开发一个程序,该程序获取 LDAP 搜索的结果并将其转换为 CSV。我以前从未使用过 LDAP,只使用过 SQL,我觉得它有点挑战性。

我无法将 LDAP 数据转换为可以很好地转换为 CSV 的表单。我一直在尝试使用我在 SQL 中学到的知识来提供帮助,但事实证明这有点挑战性。

这是我正在使用的 LDAP 服务器。

example.org

dn: dc=example,dc=org
objectClass: top
objectClass: dcObject
objectClass: organization
o: Example Inc.
dc: example

# admin, example.org
dn: cn=admin,dc=example,dc=org
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator
userPassword: mypassword

# people, example.org
dn: ou=people,dc=example,dc=org
objectClass: organizationalUnit
ou: people

# chris, people, example.org
dn: uid=jane,ou=people,dc=example,dc=org
objectClass: inetOrgPerson
cn: Chris
sn: smith
mail: chris@example.org
uid: chris

这是我用来尝试获取数据的 Ruby 代码。

ldap = Net::LDAP.new  :host => '127.0.0.1',
  :port => 1300,
  :auth => {
    :method => :simple,
    :username => 'cn=admin,dc=example,dc=org',
    :password => 'mypassword'
}
filter = Net::LDAP::Filter.eq( "cn", "Chris*")
treebase = "dc=example,dc=org"

ldap.search( :base => treebase, :filter => filter ) do |entry|
  CSV.open("mysearch.csv", "w") do |csv|
  puts "DN: #{entry.dn}"
  entry.each do |attribute, values|
    search_array.push(attribute)
    values.each do |value|
     csv << [attribute, values]
     data_array.push(values)
    end
    end
  end
end

当我尝试将其加载到 CSV 中时,我最终得到的结果如下:

如何让标题在页面顶部排列,而不是垂直排列?任何人的任何指示/建议?

【问题讨论】:

    标签: ruby csv search ldap ldap-query


    【解决方案1】:

    默认返回的结果集有点特殊: https://www.rubydoc.info/gems/ruby-net-ldap/Net/LDAP/Entry

    我会在新 CSV 文件的第一行中添加属性名称,如下所示:

    result_set = ldap.search( :base => treebase, :filter => filter )
    CSV.open("mysearch.csv", "w") do |csv|
      csv << result_set.map { |entry| entry.attribute_names }
      result_set.each do |entry|
         csv << [entry.dn] # add what you need additionally to dn https://www.rubydoc.info/gems/ruby-net-ldap/Net/LDAP/Entry)
      end
    end
    

    附:也许您需要使用 result_set.first.attribute_names 而不是地图线。

    【讨论】:

    • 对不起,这不起作用,它说“keys”是一个未定义的方法。
    • 是的,我要说这个类原来是“Net::LDAP::Entry”
    • 不幸的是,地图也无法正常工作。这是一个棘手的问题。
    • 尝试不阻塞。我会将选项添加到我的答案中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多