1

我一直在研究这段代码,但我无法让它工作。它从文本文件中获取用户信息,创建用户组并将用户放入组中。它使安全组很好,但不会将用户放入组中。没有错误消息,但经过错误检查后,将通过应添加用户并将错误放在底部的代码。有人可以帮忙吗?

文本文件中的用户名设置如下:

fred,bush,1990-20-3,123456781,2008-20-3,D5,

xin,zhao,1990-20-2,123456782,2008-20-3,D5,

bobby,bob,1990-20-1,123456783,2008-20-3,D5,

john,lose,1990-20-4,123456784,2008-20-3,D5,

elly,moose,1990-20-5,123456785,2008-20-3,D5,

jackie,chan,1990-20-6,123456786,2008-20-3,D5,

katarina,lotus,1990-20-7,123456787,2008-20-3,D5,

kelly,nunu,1990-20-8,123456788,2008-20-3,D5,

lois,harris,1990-20-9,123456789,2008-20-3,D5,

gutwold,manly,1990-20-10,123456780,2008-20-3,D5,

griswold,womenly,1990-20-11,123456710,2008-20-3,D5,

bessy,horse,1990-20-12,123456711,2008-20-3,D5,

代码是:

require 'date'
domain = "dc=TROPTRAIN,dc=net,dc=au"
ou = "ou=studentsOU"
filepath = 'C:\Documents and Settings\Administrator\My Documents\Luke Abbey Ruby Final\userfile\error_log.txt'
users = 'C:\Documents and Settings\Administrator\My Documents\Luke Abbey Ruby Final\userfile\newusers.txt'

#intro function for the program
def intro
    system "cls"
    puts ""
    puts "  Tropical Train - Adding users"
    puts "====================================================================="
    puts ""
end

#get user file
check = 0
while check == 0
    intro
    puts "Please enter the path to the user file,"
    puts "or enter the the letter 's' to use the stored file location:"
    puts ""
    puts "#{users}"
    userfile = gets.chomp.to_s
    if ((userfile == 's') || (userfile == 'S'))
        check = 1
        userfile = users

        #puts userfile
        #system "pause"
    elsif(test(?e,userfile))
        check = 1
    elsif userfile =~ (/[\x00\/\\:\*\?\"<>\|]/)
        puts "The userfile contains invalid characters. Try again."
        system "pause"
        check = 0
    elsif userfile == ""
        puts "You entered nothing. Try again."
        system "pause"
        check = 0
    elsif(!test(?e,userfile))
        check = 0
        puts "The file #{userfile} does not exist. Try again."
        system "pause"
    else 
        puts "Your entry is invalid. Try again."
        system "pause"
        check = 0
    end
end

#create the security groups via DOS and check wheteher they already exist
grpC2 = 0
grpC3 = 0
grpC4 = 0
grpD5 = 0
expired = 0
students = 0
%x[dsquery group "dc=troptrain,dc=net,dc=au"].each do |line|
    data = line
    if line.include?("grpC2")
        grpC2 = 1
    end
    if line.include?("grpC3")
        grpC3 = 1
    end
    if line.include?("grpC4")
        grpC4 = 1
    end
    if line.include?("grpD5")
        grpD5 = 1
    end
    if line.include?("Expired")
        expired = 1
    end
    if line.include?("Students")
        students = 1
    end
end
if grpC2 != 1
    %x[dsadd group "cn=grpC2,ou=studentsOU,dc=troptrain,dc=net,dc=au"]
end
if grpC3 != 1
    %x[dsadd group "cn=grpC3,ou=studentsOU,dc=troptrain,dc=net,dc=au"]
end
if grpC4 != 1
    %x[dsadd group "cn=grpC4,ou=studentsOU,dc=troptrain,dc=net,dc=au"]
end
if grpD5 != 1
    %x[dsadd group "cn=grpD5,ou=studentsOU,dc=troptrain,dc=net,dc=au"]
end
if expired != 1
    %x[dsadd group "cn=Expired,ou=studentsOU,dc=troptrain,dc=net,dc=au"]
end
if students != 1
    %x[dsadd group "cn=Students,ou=studentsOU,dc=troptrain,dc=net,dc=au"]
end

#open the file
f1 = File.open(userfile,"r")

#read the file line by line
count = 0
f1.each do |line|
    data = line.split(',')
    firstname = data[0].capitalize
    surname = data[1].capitalize
    dob = data[2].to_s
    snumber = data[3].to_s
    enddate1 = data [4].gsub!("-","/")
    enddate = enddate1
    area = data[5]
    fullname = (firstname + (" ") + surname)
    group = "cn=#{'grp'+data[5]}"
    password1 = data[2].gsub("-", "")
    password = password1
    count = count + 1
    #display values for establishing error checks
    #puts ""
    #puts "fullname:   #{fullname}"
    #puts "dob:        #{dob}"
    #puts "snumber:    #{snumber}"
    #puts "enddate:    #{enddate}"
    #puts "area:       #{area}"
    #puts "password:   #{password}"
    #puts "group:      #{group}"
    #puts "count:      #{count}"
    #puts ""
    if ((data[2] =~ /[^0-9-]/) || (data[3] =~ /[^0-9-]/) || (data[4] =~ /[^0-9\/]/) ||    (data[5] =~ /[^A-Za-z0-9 ]/) || (data[0] =~ /[^A-Za-z ]/) || (data[1] =~ /[^A-Za-z ]/) || (data[0] == "") || (data[1] == "") || (data[2] == "") || (data[3] == "") || (data[4] == "") || (data[5] == "") || (data[6] == ""))
        puts ""    
        puts "====================================================================="
        puts "User account No #{count}: #{fullname} was not created."
        puts ""
        if (File.exist?(filepath))
            file = File.open((filepath), "a+")
            t = Time.now    
            file.puts "====================================================================="
            file.puts "User account No #{count}: #{fullname} was not created."
            file.puts t
            file.puts ""
        else
            puts "Error log cannot be found!"
            puts "Searched directory: #{filepath}"
        end

        #checking database entries for errors
        if ((data[0] == "") || (data[0] =~ /[^A-Za-z ]/))
            puts "No/Incorrect firstname on file."
            file.puts "No/Incorrect firstname on file."
        end     
        if ((data[1] == "") || (data[1] =~ /[^A-Za-z ]/))
            puts "No/Incorrect surname on file."
            file.puts "No/Incorrect surname on file."
        end
        if ((data[2] == "") || (data[2] =~ /[^0-9-]/))
            puts "No/Incorrect birthdate on file."
            file.puts "No/Incorrect birthdate on file."
        end
        if ((data[3] == "") || (data[3] =~ /[^0-9-]/))
            puts "No/Incorrect student number on file."
            file.puts "No/Incorrect student number on file."
        end
        if ((data[4] == "") || (data[4] =~ /[^0-9\/]/))
            puts "No/Incorrect study end date on file."
            file.puts "No/Incorrect study end date on file."
        end
        if ((data[5] == "") || (data[5] =~ /[^A-Za-z0-9 ]/))
            puts "No/Incorrect study area on file."
            file.puts "No/Incorrect study area on file."
        end
        file.close
    else
        begin
            puts "working"
            system 'pause'
            ad = WIN32OLE.connect("LDAP://#{ou},#{domain}")
            user = ad.create("user","cn=#{fullname}")
            user.givenName = firstname
            user.Sn = surname
            user.SAMAccountname = "#{snumber}"
            user.userPrincipalName = "#{snumber}@Troptrain.net.au"
            user.displayName = fullname
            user.profilePath = "c:\\profiles\\students\\logon"
            user.setInfo

            user_fqdn = "cn=#{fullname},#{ou},#{domain}"
            user = WIN32OLE.connect("LDAP://#{user_fqdn}")
            user.userPassword = Password01
            user.accountDisabled = 0
            user.accountExpirationDate = enddate

            user.setInfo

            studentgroup_fqdn = "#{group},#{ou},#{domain}"
            grp = WIN32OLE.connect("LDAP://#{studentgroup_fqdn}")
            grp.add("LDAP://#{user_fqdn}")

            allgroup_fqdn = "cn=students,#{ou},#{domain}"
            allgrp = WIN32OLE.connect("LDAP://#{allgroup_fqdn}")
            allgrp.add("LDAP://#{user_fqdn}")

            puts ""
            puts "====================================================================="
            puts "User account No #{count}: #{fullname} was created."
            sleep 0.5

        rescue

        end
    end
end
puts "====================================================================="
puts ""
puts "See Error log for details."
puts ""
puts "The program has finished creating users."
f1.close
4

1 回答 1

2

分开你的顾虑。

  1. 您需要解析文本文件并以结构化方式从中检索用户和组。
  2. 您需要导入您的组,然后导入您的用户。
  3. 然后,您将用户分配给您创建的组。
  4. 每个用户和组必须位于他们自己的 OU 中才能识别它们。
  5. 这些 OU 必须是已知的。

现在向后工作并分解出您需要继续进行的所有内容。

  1. 找到要绑定到 LDAP 服务器的主机名、端口和凭据。
  2. 找到您的基本 DN。( DC=troptrain, DC=net, DC=au)
  3. 找到您的用户和组前缀。(基础 DN 之后的 OU 段)。
  4. 导入您的数据。
  5. 核实。(这也很重要!)

由于 LDAP 与典型的关系数据库相比似乎有些落后,因此我建议将 ActiveLDAP ruby​​ gem 与gem install active_ldap. 它使使用 LDAP 变得容易得多。

#!/usr/bin/env ruby

require 'active_ldap'

class User < ActiveLdap::Base
  ldap_mapping :dn_attribute => 'CN', :prefix => '<user-ou-prefix>',
  has_many :groups, :class => 'Group', :wrap => 'memberOf', :primary_key => 'DN'
end

class Group < ActiveLdap::Base
  ldap_mapping :dn_attribute => 'CN', :prefix => '<group-ou-prefix>',
  has_many :members, :class => 'User', :wrap => 'member', :primary_key => 'DN' 
end

ActiveLdap::Base.setup_connection(
  :host => '<hostname>',
  :base => '<base-dn>',
  :bind_dn  => '<bind-dn>',
  :password => '<bind-password>',
  :allow_anonymous => false,
  :try_sasl => false,
  :port => 389
)

从那里,您应该能够使用类似 ActiveRecord 的界面创建、读取、更新和删除用户。调用#setup_connection建立用于连接的选项,并且has_many调用在您的类之间建立类似外键的关系。

# Find and display all users:
User.find(:all, '*') do |user|
  puts user.cn
end

# Create a group with a CN of 'Anonymous'
group = Group.new('Anonymous')
group.description = "An anonymous group."
group.save

# Change the user with the CN 'Nobody' and change their displayName attribute.
user = User.find('Nobody')
user.displayName = "Nobody's Name"
user.save

# Delete a group with a CN of 'Foobar'.
group = Group.find('Foobar')
group.delete

ActiveLdap 使得从 Ruby 操作 LDAP 变得非常容易,我强烈推荐它。

于 2012-06-20T03:49:36.863 回答