首先我想写的是我正在学习编写脚本。我在 Ubuntu 22.04 ADDC Samba 上设置的 VBox 中有一个测试域“universum.local”。我想使用 bash 脚本查询域控制器以获取域用户列表 (10),并将有关它们的数据保存到 json 文件中。目前我能够获取必要的信息并将其保存到 txt 文件中。
这是我的脚本代码:
#!/bin/bash
clear
ldapuserslistfilename="ldapuserslist.txt"
ldapuserslistfile="$tmp/$ldapuserslistfilename"
ldapusersinfofilename="ldapusersinfo.txt"
ldapusersinfofile="$tmp/$ldapusersinfofilename"
# main code
touch $ldapuserslistfile
touch $ldapusersinfofile
samba-tool user list > $ldapuserslistfile
while read -r line ; do
for user in $line ; do
samba-tool user show $user >> $ldapusersinfofile
done
done < $ldapuserslistfile
# copying txt files for tests
cp $ldapuserslistfile /mnt
cp $ldapusersinfofile /mnt
# deleting files
if [ -f $ldapuserslistfile ] ; then rm -f $ldapuserslisfile ; fi
if [ -f $ldapusersinfofile ] ; then rm -f $ldapusersinfofile ; fi
有输出,所有用户都保存在txt文件中,格式如下:
dn: CN=Bruce Banner,OU=Users,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Bruce Banner
sn: Banner
givenName: Bruce
instanceType: 4
whenCreated: 20220926075536.0Z
whenChanged: 20220926075536.0Z
displayName: Bruce Banner
uSNCreated: 4128
name: Bruce Banner
objectGUID: d1fb86d4-17bc-43f2-af83-ca06fa733e9e
badPwdCount: 0
codePage: 0
countryCode: 0
badPasswordTime: 0
lastLogoff: 0
lastLogon: 0
primaryGroupID: 513
objectSid: S-1-5-21-2846706046-4262971904-2743650290-1109
accountExpires: 9223372036854775807
logonCount: 0
sAMAccountName: hulk
sAMAccountType: 805306368
userPrincipalName: [email protected]
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=universum,DC=local
pwdLastSet: 0
userAccountControl: 512
uSNChanged: 4132
memberOf: CN=Avengers,OU=Groups,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
distinguishedName: CN=Bruce Banner,OU=Users,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
我想要 json 格式的数据,例如
{
"users": [
{
"cn" : "Bruce Banner",
"sn" : "Banner",
"givenName" : "Bruce",
"whenCreated" : "20220926075536.0Z",
"<objectname>" : "<value>",
"<objectname>" : "<value>",
},
{
<next user info>
},
{
<next user info>
}
]
}
objectname 是下一个用户项,如lastLogon、lastLogoff 等。我想将所有用户保存在json 文件中,以便我可以使用计算机上的另一个powershell 脚本读取它们
更新:
我添加了下面几行
# conversion fron txt to json
jsonfilename="jsontestfile.json"
json="./$jsonfilename"
touch $json
ed -s $ldapusersinfofile << 'EOF' > $json
v/^cn:\|^sn:\|^givenName:\|^displayName:\|^name:\|^whenCreated:/d
,s/^\(.*[^:]*\): \(.*\)/"\1": "\2"/
g/cn\|sn\|givenName\|displayName\|name\|whenCreated/s/$/,/
,s/^/ /
g/lastLogon/t. \
s/.*/},/g
1,$-1g/}/t. \
s/.*/{/g
0a
{
.
$s/,//
,p
Q
EOF
在#main代码部分和#copying txt文件进行测试部分之间,我已经输出到json文件,如
{
"cn": "James Rhodes",
"sn": "Rhodes",
"givenName": "James",
"whenCreated": "20220926075852.0Z",
"displayName": "James Rhodes",
"name": "James Rhodes",
"lastLogon": "0"
},
{
"cn": "T'Chala",
"givenName": "T'Chala",
"whenCreated": "20220926081521.0Z",
"displayName": "T'Chala",
"name": "T'Chala",
"lastLogon": "0"
},
{
"cn": "Stephen Strange",
"sn": "Strange",
"givenName": "Stephen",
"whenCreated": "20220926080942.0Z",
"displayName": "Stephen Strange",
"name": "Stephen Strange",
"lastLogon": "0"
}
为了能够读取我的 powershells 脚本中的 json 文件,缺少
{
"users": [
在开始数据和
]
}
在数据末尾有类似的文件
{
"users": [
{
"cn": "James Rhodes",
"sn": "Rhodes",
"givenName": "James",
"whenCreated": "20220926075852.0Z",
"displayName": "James Rhodes",
"name": "James Rhodes",
"lastLogon": "0"
},
{
"cn": "T'Chala",
"givenName": "T'Chala",
"whenCreated": "20220926081521.0Z",
"displayName": "T'Chala",
"name": "T'Chala",
"lastLogon": "0"
},
{
"cn": "Stephen Strange",
"sn": "Strange",
"givenName": "Stephen",
"whenCreated": "20220926080942.0Z",
"displayName": "Stephen Strange",
"name": "Stephen Strange",
"lastLogon": "0"
}
]
}
通过PS脚本阅读
Clear
$json = Get-Content <pathToFile>\jsontestfile.json -Raw | ConvertFrom-Json
foreach ($user in $json.users){
echo $user.cn
echo $user.sn
echo $user.givenName
echo "----------"
}
如何添加缺失的字符?
您需要“jq”,然后解析 samba-tool 的输出,如下所示:
sudo samba-tool user show $USERNAME -H ldap://<DC_SHORT_HOSTNAME> -d0 | jq -R -s -c 'split("\n")'
您可以轻松地在用户名列表中使用“for 循环”。
我将代码更改为:
# add prefix
printf '{\n"users" :\n[\n' > $json
# add converted users data
ed -s $data << 'EOF' >> $json
v/^cn:\|^sn:\|^givenName:\|^whenCreated:\|^displayName:\|^name:\|^badPwdCount:\|^badPasswordTime:\|^lastLogoff:\|^primaryGroupID:\|^accountExpires:\|^sAMAccountName:\|^userPrincipalName:\|^pwdLastSet:\|^userAccountControl:\|^lastLogonTimestamp:\|^whenChanged:\|^lastLogon:\|^logonCount:\|^distinguishedName:/d
,s/^\(.*[^:]*\): \(.*\)/"\1" : "\2"/
g/cn\|sn\|givenName\|whenCreated\|displayName\|name\|badPwdCount\|badPasswordTime\|lastLogoff\|primaryGroupID\|accountExpires\|sAMAccountName\|userPrincipalName\|pwdLastSet\|userAccountControl\|lastLogonTimestamp\|whenChanged\|lastLogon\|logonCount/s/$/,/
,s/^/ /
g/distinguishedName/t. \
s/.*/},/g
1,$-1g/}/t. \
s/.*/{/g
0a
{
.
$s/,//
,p
Q
EOF
# add suffix
printf ']\n}' >> $json
现在可以使用了,Powershell 可以读取我的文件。