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 に答える