从域或子域为 LDAP 自动创建 DC= 字段

问题描述 投票:0回答:1

通过 BASH 脚本,我会自动构建一个字符串,例如:

DC=us,DC=earth,DC=com
for LDAP 域组件。但是,我不知道一个域可能有多少个子域组件

例如:

  • dub.sub.domain.tld
    需要
    DC=dub,DC=sub,DC=domain,DC=tld
    
    
  • sub.domain.tld
    需要
    DC=sub,DC=domain,DC=tld
    
    
  • domain.tld
    需要
    DC=domain,DC=tld
    
    
但是,我不知道我会拥有哪一个,而且我需要自动创建这个字符串。

我目前的解决方案是:

# The variable we got somehow indomain="dub.sub.domain.tld" #IFS='.' #Please no, sed instead... # Create a loopable list with space as the IFS domaincomponents=$(echo $indomain | sed 's/\./ /g') # Process each domain component finalstring="" for component in $domaincomponents; do finalstring=DC=$component,$finalstring done # Remove the trailing , finalstring=$(echo $finalstring | sed 's/\(.*\),/\1 /')
结果:

$finalstring

 = 
DC=dub,DC=sub,DC=domain,DC=tld


这可行,但似乎处理过度,像

awk

 这样的工具可以做得更干净。

有更好的方法还是这是最好的?

我假设LDAP中的DC=

可以深入到任何子域名;如果我错了请纠正我。

for-loop awk sed ldap subdomain
1个回答
1
投票

sed

echo 'dub.sub.domain.tld' | sed 's/^/DC=/; s/\./,DC=/g'
输出:

DC=dub、DC=sub、DC=域、DC=tld
    
© www.soinside.com 2019 - 2024. All rights reserved.